The Wayback Machine - https://web.archive.org/web/20240817022827/https://www.geeksforgeeks.org/how-to-read-pdf-file-using-php/
Open In App

How to read PDF file using PHP ?

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will learn how you can show/read PDF file contents on a browser using PHP.

We have to include an external PHP file named “class.pdf2text.php“. Include it in the required web page using PHP. Create an HTML form, in which we can choose a PDF file from your computer and also check whether its file extension is PDF or not.

Approach: Make sure you have a XAMPP server or WAMP server installed on your machine. In this article, we will be using the XAMPP server.

Project folder structure and files:: Create a folder for your project and add class.pdf2text.php. Create a new index.php file. Keep your main project folder (for example.. example/pdf here) in the “C://xampp/htdocs/” if you are using XAMPP or “C://wamp64/www/” folder if you are using the WAMP server respectively. 

The folder structure should look like this:

folder structure

index.php: Below is the PHP source code for attaching the pdf file using HTML form and reading its content. Let us first understand the PHP part.

In the below code, the first if block verifies whether there is any file attached or not using the PHP isset() function. And the second if block verifies that the uploaded file is a PDF file. $_FILES is a two-dimensional superglobal associative array of items that are being uploaded via the HTTP POST method. Then we are instantiating the pdf2text() method in $a and finally return the contents of the pdf file.

In the HTML <form> tag, we are using “enctype=’multipart/form-data” which is an encoding type that allows files to be sent through a POST method. Without this encoding, the files cannot be sent through the POST method. We must use this enctype if you want to allow users to upload a file through a form.

PHP
<?php
 
require('class.pdf2text.php');
extract($_POST);

if(isset($readpdf)){
    
    if($_FILES['file']['type']=="application/pdf") {
        $a = new PDF2Text();
        $a->setFilename($_FILES['file']['tmp_name']); 
        $a->decodePDF();
        echo $a->output(); 
    }
     
    else {
        echo "<p style='color:red; text-align:center'>
            Wrong file format</p>
";
    }
}    
?>

<html>

<head>
    <title>Read pdf php</title>
</head>

<body>
    <form method="post" enctype="multipart/form-data">
        Choose Your File
        <input type="file" name="file" />
        <br>
        <input type="submit" value="Read PDF" name="readpdf" />
    </form>
</body>

</html>

Output: Finally, you should be able to read the contents of the PDF file on the browser.


PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Similar Reads

Create a Read More Read Less Button using HTML CSS and JavaScript
The "read more - read less" button is a commonly used feature that enhances user experience by providing a convenient way to reveal or hide content. The article covers two distinct approaches, including Display Inline and Display Block. Table of Content Using Display InlineUsing Display BlockUsing Display InlineCreate a separate HTML, CSS, and Java
4 min read
How to create a table in PDF file from external text files using PHP ?
In this article, we will learn to create a table in a PDF file from an external text file with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and man
3 min read
How to generate PDF file and add TrueType fonts using PHP ?
In this article, we will learn how to generate PDF files and add new TrueType fonts with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more
2 min read
How to generate PDF file using PHP ?
In this article, we will learn how to generate PDF files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more. Approach: You need to dow
2 min read
How to generate PDF file from external text files using PHP ?
In this article, we will learn how to generate PDF files from external text files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more.
5 min read
How to read a Large File Line by Line in PHP ?
We will use some file operations to read a large file line by line and display it. Read a file: We will read the file by using fopen() function. This function is used to read and open a file. Syntax: fopen("filename", access_mode); Parameter: filename: Filename is the name of the file access_mode: It is the mode of the file that includes r - read m
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
Read XML file and print the details as tabular data by using JavaScript
To read the XML file and print the details of an XML file in a Tabular form by using JavaScript. We need to create an XML file with the data we want to print. XML stands for Extensible Markup Language· It is a markup language very similar to HTML. The main purpose of the XML file is to store and transport the data. Creating an XML file is very simp
4 min read
How to Read a File Line by Line using Node.js ?
The ability to Read a File Line by Line using Node allows us to read large files without entirely storing them in memory. It is useful in saving resources and improving the efficiency of the application. It allows us to look for the information that is required and once the relevant information is found, we can stop the search process and can preve
3 min read
Explain about Read and Write of a file using JavaScript
Handling file operations such as reading and writing is a common requirement in many JavaScript applications, especially on the server side using Node.js. This article explains how to perform these operations using Node.js's built-in fs (File System) module. The fs ModuleThe fs module provides an API for interacting with the file system in a manner
3 min read
How to read a local text file using JavaScript?
Imagine your web page wants to interact with files on the user's computer. HTML 5 provides a handy tool called the File API to make this possible. The File API allows interaction with single, multiple as well and BLOB files. The FileReader API can be used to read a file asynchronously in collaboration with JavaScript event handling. However, all th
3 min read
How to read and write JSON file using Node ?
Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises. JSON(JavaScript Object Notation) is a simple and text-based format for exchanging data between diff
3 min read
Read JSON File using JavaScript
JSON stands for JavaScript Object Notation. It's a way of organizing data in a script file using text, making it easy to store and share data. Reading JSON files, whether they're stored locally or on a server, is crucial for web applications. In this tutorial, we'll cover three methods for reading JSON files in JavaScript, which can be really helpf
4 min read
How to generate PDF file using jsPDF library ?
In most web-based projects or applications, we need report generation for showing some relevant data which includes PDF conversion. There are many PDF generation tools available from raw HTML data, CSV, or any database available. The jsPDF library is one of the generation tools commonly used in web applications. In this article, we will learn how t
3 min read
How to Download PDF File on Button Click using JavaScript ?
Sometimes, a web page may contain PDF files that can be downloaded by the users for further use. Allowing users to download the PDF files can be accomplished using JavaScript. The below methods can be used to accomplish this task. Table of Content Using html2pdf.js libraryUsing pdfmake LibraryUsing window.print() methodUsing html2pdf.js libraryFirs
5 min read
How to Embed PDF file using HTML ?
We will learn how to embed PDF files in HTML documents and explore their implementation through examples. Sometimes, you may want to insert a PDF file into an HTML document to make the content more interactive. Since HTML and PDF formats are different, embedding PDFs can be a bit challenging. Here are some methods to accomplish this task: These are
3 min read
How to open PDF file in new tab using ReactJS ?
React JS, a commonly used JavaScript library for building user interfaces, sometimes requires opening PDF files in a new tab or window when a button is clicked. You can make this happen by bringing the PDF file into your React application as a module. Prerequisites:React JSFunctional Component in ReactApproach to open PDF File: To complete this tas
2 min read
CRUD Operation in MySQL Using PHP, Volley Android - Read Data
In the previous article, we have performed the insert data operation. In this article, we will perform the Read data operation. Before performing this operation first of all we have to create a new PHP script for reading data from SQL Database. Prerequisite: You should be having Postman installed in your system to test this PHP script. Create a PHP
9 min read
How to read and write Excel file in Node.js ?
Node.js is an open-source and cross-platform JavaScript runtime environment that can also be used to read from a file and write to a file which can be in txt, ods, xlsx, docx, etc format. The following example covers how an excel file(.xlsx) file is read from an excel file and then converted into JSON and also to write to it. It can be achieved usi
3 min read
Javascript | Program to read text File
Given a text file, write a JavaScript program to extract the contents of that file. There is a built-in Module or in-built library in NodeJs that handles all the reading operations called fs (File-System). It is basically a JavaScript program (fs.js) where a function for reading operations is written. Import fs-module in the program and use functio
2 min read
How to create a simple web server that can read a given file on a given disk via “readFile” function ?
To create a web server we need to import the 'http' module using require() method and create a server using the createServer() method of the HTTP module. Syntax: const http = require('http')const server = createServer(callback).listen(port,hostname);Now in the callback in the createServer() method, we need to read the file from our local directory
3 min read
How to download PDF file in ReactJS ?
To download pdf in React JS there are methods given in JavaScript and HTML DOM. They provide a convenient way to download files and store them in local computers. Here we will use locally stored pdf files in the project folder to use as a sample pdf downloaded by the browser. These are the approaches to download a PDF in React JS projects. Table of
3 min read
How to add HTML and CSS into PDF File ?
HTML and CSS are regularly converted into PDF format during web development. PDFs enable the creation of printable documents, information exchange across several platforms, and preserving a webpage's original layout. Several JavaScript libraries can help us to complete our tasks. Libraries like html2pdf and jsPDF are well-known for converting webpa
3 min read
PHP | Gmagick read() Function
The Gmagick::read() function is an inbuilt function in PHP which is used to read an image from your local folder and add it to the Gmagick object. Syntax: Gmagick Gmagick::read( string $filename ) Parameters: This function accepts a single parameter $filename which holds the name of the file. Return Value: This function returns a Gmagick object con
1 min read
PHP | XMLReader read() Function
The XMLReader::read() function is an inbuilt function in PHP which is used to move to next node in document. Thus this function is used to traverse through the XML document. Syntax: bool XMLReader::read( void ) Parameters: This function doesn’t accepts any parameter. Return Value: This function returns TRUE on success or FALSE on failure. Below giv
2 min read
How to Read and Write from Files in PHP ?
In PHP, reading from and writing to files is a common task in web development, especially for tasks such as reading configuration files, processing data files, or logging information. PHP provides built-in functions for handling file operations, making it straightforward to perform file read and write operations. Reading from a File:Use fopen() to
1 min read
How to read user or console input in PHP ?
In PHP, the console is a command-line interface, which is also called interactive shell. We can access it by typing the following command in a terminal: php -a If we type any PHP code in the shell and hit enter, it is executed directly and displays the output or shows the error messages in case of any error. A sample run of a PHP code, that reads i
4 min read
How to read each character of a string in PHP ?
A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHP Table of Content Using str_split() method - The str_split() methodUsing strlen() method - The strlen
4 min read
PHP | FPDF-PDF Generator
FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files. The main features of this class are: Allows to setup page format and margins. Allows to setup page header and footer. It provides auto
2 min read
How to convert a PDF document to a preview image in PHP?
Converting a PDF document into a set of images may not sound that fun, but it can have a few applications. As the content from images cannot be copied that easily, the conversion makes the document strictly 'read-only' and brings an extra layer of protection from plagiarism. The images may also come in handy when you need some ready-made slides for
6 min read
three90RightbarBannerImg