Skip to content
Related Articles

Related Articles

Improve Article

How to read PDF file using PHP ?

  • Last Updated : 14 Jul, 2021
Geek Week

In this article, we will learn how you can show PDF file content 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.

Project Structure: Create a folder for your project and add class.pdf2text.php and create a new index.php file.

FILE structure

Example:

index.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: The following output is in video format for the above.

read PDF file

My Personal Notes arrow_drop_up
Recommended Articles
Page :