Generate Thumbnail from PDF using PHP

PDF Thumbnail is the best way to display a preview of the PDF document. When you have a media library section in the web application and want to display the PDF preview in a listing, thumbnail images are a great option. In this process, you need to create thumbnail image from PDF document and display this image as a preview.

There are various third-party libraries available to generate thumbnail of PDF. But you can create PDF thumbnails in PHP without using any third-party library. PHP ImageMagick library can be used to generate PDF thumbnails in PHP. In this tutorial, we will show you how to generate thumbnail from PDF using PHP.

In this PDF thumbnail generator script, we will use the ImageMagick extension to generate thumbnail images from PDF in PHP. Also, we will show how you can upload PDF files and create thumbnails on the fly using PHP.

ImageMagick Library

The imagick extension must be installed and enabled on the server to make this script work. Check whether the following line is available in the php.ini file.

extension=php_imagick.dll

You can also check the availability of the Imagick extension on your server by running the phpinfo() command on the browser.

imagick-extension-phpinfo-codexworld

Generate Thumbnail from PDF in PHP

The following code snippet creates thumbnail image for PDF file using PHP.

  • Specify the source PDF file from which the thumbnail image will be generated.
  • Specify the folder path where the PDF thumbnail will be stored.
  • Initialize the Imagick library and pass the PDF document page number in the Imagick() class.
  • Set the thumbnail image file type using the setImageFormat() function of the Imagick library.
  • Set the thumbnail image size using the thumbnailImage() function.
  • Save image in the directory using the writeImage() function.
// Source PDF file 
$pdf_file 'pdf_files/Brochure.pdf';

// PDF thumbnail image path
$thumb_path 'thumbnails/pdf-image.jpg';

// Generate thumbnail from PDF
$im = new Imagick($pdf_file."[0]"); // 0-first page, 1-second page
$im->setImageColorspace(255); // prevent image colors from inverting
$im->setImageFormat("jpeg"); // file type
$im->thumbnailImage(10241024); // width and height
$im->writeImage($thumb_path);
$im->clear();
$im->destroy();

Upload PDF File and Create Thumbnail of PDF

This example code snippet shows you the step-by-step process to upload PDF files and generate thumbnails from PDF using PHP.

HTML Form:
Define HTML elements for file uploading form.

<form action="submit.php" method="post" enctype="multipart/form-data">
    <div class="form-input">
        <label for="pdf_file">PDF File</label>
        <input type="file" name="pdf_file" placeholder="Select a PDF file" required="">
    </div>
    <input type="submit" name="submit" class="btn" value="Create Thumbnail">
</form>

On form submission, the selected file is submitted to the server-side script for process further.

Server-side Script (submit.php) to Extract Text from Uploaded PDF:
The following code is used to upload the submitted file and generate thumbnail of PDF.

  • Retrieve file name using $_FILES in PHP.
  • Get file extension using pathinfo() function with the PATHINFO_EXTENSION filter.
  • Validate the file to check whether it is a valid PDF file.
  • Retrieve file path using tmp_name in $_FILES.
  • Generate thumbnail of the PDF first page using the Imagick library and save the image in a folder.
<?php 

$statusMsg 
'';
if(isset(
$_POST['submit'])){
    
// If file is selected
    
if(!empty($_FILES["pdf_file"]["name"])){
        
// File upload path
        
$fileName basename($_FILES["pdf_file"]["name"]);
        
$fileType pathinfo($fileNamePATHINFO_EXTENSION);
        
        
// Allow certain file formats
        
$allowTypes = array('pdf');
        if(
in_array($fileType$allowTypes)){
            
// Source PDF file
            
$pdf_file $_FILES["pdf_file"]["tmp_name"];

            
// PDF thumbnail image path
            
$thumb_path 'pdf_thumbnails/pdf-image.jpg';

            
// Generate thumbnail from PDF
            
$im = new Imagick($pdf_file."[0]"); // 0-first page, 1-second page
            
$im->setImageColorspace(255); // prevent image colors from inverting
            
$im->setImageFormat("jpeg"); // file type
            
$im->thumbnailImage(10241024); // width and height
            
$im->writeImage($thumb_path);
            
$im->clear();
            
$im->destroy();
        }else{
            
$statusMsg '<p>Sorry, only PDF file is allowed to upload.</p>';
        }
    }else{
        
$statusMsg '<p>Please select a PDF file to generate thumbnail.</p>';
    }
}

echo 
$statusMsg;

?>

Conclusion

Here we discussed the simple way to create PDF thumbnail image with PHP. You can generate PDF thumbnails in different image formats (JPG, PNG, etc) and sizes. This PDF thumbnail maker script functionality can be enhanced for advanced features.

Extract Text from PDF using PHP

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up