How to read PDF file using PHP ?
Last Updated :
26 Jul, 2024
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 structureindex.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
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, color
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 br
5 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
2 min read
Download file from URL using PHP In this article, we will see how to download & save the file from the URL in PHP, & will also understand the different ways to implement it through the examples. Below are the approaches to download file from URL using PHP:Table of ContentUsing file_get_contents() functionUsing PHP CurlUsing
3 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: I
2 min read