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.
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.
The following code snippet creates thumbnail image for PDF file using PHP.
// 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(1024, 1024); // width and height
$im->writeImage($thumb_path);
$im->clear();
$im->destroy();
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.
pathinfo()
function with the PATHINFO_EXTENSION filter.tmp_name
in $_FILES.<?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($fileName, PATHINFO_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(1024, 1024); // 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;
?>
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