Pdf thumbnails / Pdf to jpg

Martin Zellerphp 2 Comments

Do you want to create thumbnails for pdf files with PHP (or C#)? You can simply do this with ghostscript. Here’s the php way (it is very easy to convert this code to C#):

  1. First install ghostscript on your server. Download: http://ghostscript.com/releases/
  2. Then write your code:
[php]$pathToPdf = "C:\….\PdfFile.pdf";
$pathToJpg = "C:\….\generatedThumbnail.jpg";

$gsCall = "\"C:\….\gs\gs8.70\bin\gswin32.exe\" -q -dBATCH -dMaxBitmap=300000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dFirstPage=1 -dLastPage=1 -sOutputFile={0} {1} -c quit";

$gsCall = str_replace(array(‘{0}’, ‘{1}’), array($pathToJpg, $pathToPdf), $gsCall); // or use sprintf
$str = exec($gsCall, $output=array(), $returnValue);[/php] This code will generate a jpg file with the content of the first page of the pdf file.

Ready.

Comments 2

  1. läuft ABER bei mir werden die bilder vom pdf nicht im jpeg angezeigt, wieso?

    i am using convert command in php too, but the images from the pdf are not showing in my created jpg or png, why?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.