PHP upload and create PDF preview Image

In most of the website for different for different reasons we have to give option to upload a PDF file. It’s bit difficult to preview an uploaded PDF file. So in this tutorial I will explain you how to create a PDF preview image using PHP.

PHP upload and create PDF preview Image

You can view demo and download how to create PDF Preview Image using PHP:

Live Demo Download

Create a PDF Uploader:

First we are going to create a PDF uploaded in order to get the preview image using PHP. Let’s begin with a HTML form.

<!DOCTYPE html>
      <meta charset="utf-8" />
      <title>PDF Preview Image</title>
      <form method="post" enctype="multipart/form-data">
      <p><input type="file" name="pdfupload" id="pdfupload"></p>
      <p><input type="submit" name="submit" value="Upload" /></p>

Upload the PDF:

Now upload the PDF and generate a preview image.

//Define the directory to store the uploaded PDF
$pdfDirectory = "pdf/";

//Define the directory to store the PDF Preview Image
$thumbDirectory = "pdfimage/";
//Get the name of the file (Basename)
$filename = basename( $_FILES['pdfupload']['name'], ".pdf");

// Clean the filename
//Remove all characters from the file name other than letters, numbers, hyphens and underscores
$filename = preg_replace("/[^A-Za-z0-9_-]/", "", $filename).".pdf";
//Name the thumbnail image (Same as the pdf file -  You can set custom name)
$thumb = basename($filename, ".pdf");
 //Upload the PDF
    if(move_uploaded_file($_FILES['pdfupload']['tmp_name'], $pdfDirectory.$filename)) {
    //Set path to the PDF file
    $pdfWithPath = $pdfDirectory.$filename;
    //Add the desired extension to the thumbnail
    $thumb = $thumb.".jpg";
    //execute imageMagick's 'convert', setting the color space to RGB
    //This will create a jpg having the widthg of 200PX
    exec("convert ".{$pdfWithPath}[0]." -colorspace RGB -geometry 200 $thumbDirectory$thumb");
    // Finally display the image
    echo '<p><a href="'.$pdfWithPath.'"><img src="pdfimage/'.$thumb.'" alt="" /></a></p>';

Bottom Line:

Kindly make sure that your domain will support for executing the exec functions in your server. If its disabled you can enable it in php.ini. If not contact your domain provider.

Hope that this article will be helpful for you. Don’t forget to make a free email subscription for the new updation.

3 Comments PHP upload and create PDF preview Image

  1. qurina

    Hello your code is awesome. It helped me for image upload preview but still even though i extended in php.ini if i uploaded pdf file its preview image doesn’t show neither in your demo.
    Can u help me for that solution?

    1. Prasad G K

      Hi Priyanka, I just verified that it’s working in demo. Can you please check in demo page? If not try with different pdf file. Please let me know if anything. Thanks


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>