PDF Thumbnail

Permalink 2 users found helpful
Has anyone heard if/how you can programmatically generate a thumbnail of a PDF document?

View Replies: View Best Answer
Fernandos replied on at Permalink Best Answer Reply
hi Ricalsin!

You need ImageMagick and GhostScript installed to do that!

$im = new imagick('file.pdf[0]');
$im->setImageFormat( "jpg" );
$im->setResolution(300, 300);
header( "Content-Type: image/jpeg" );
echo $im;

The [0] means page 1.
Get the page count using:

You can also create thumbs of all pages using a loop:

I would recommend FPDF if you want to create PDF's on the fly.

Ricalsin replied on at Permalink Reply
Hello Fernando,

Thanks for the great information.

I was geared to implement TCPDF, but I'll check out your suggestion of FPDF. I'm generating data into the db and then building PDFs for download and use by the customer.

Thanks again, very helpful.

Fernandos replied on at Permalink Reply
Yeah.. I had a complex app that generated a quite complicated pdf attached to an email as order confirmation for bookings on different hotelchains.

A hint: Don't keep the generated pdf's in your database, save them in a protected directory or keep them in /tmp and wipe em after some "time" after they got served.

Keeping the pdfs in your DB creates unneccessary requests and overhead.
Keeping the pdfs persistantly will sooner or later eat up your hdds. So give them a lifetime or just temporarily cache them for 6h or something. I didn't keep them on disk, because they got emailed and that's why I didn't needed to keep a copy. My mail server had a copy in the sent directory which I applied some rules to...

I tried TCPDF and some other libs, but you will be lucky there is fpdf even if it doesn't feature everything that tcpdf does, it's a lot better in my opinion.
cannonf700 replied on at Permalink Reply
Can you give an example of how you would implement this in a view.php file? I'm trying to make a list of files in a certain file set and display the thumbnail of the pdf file.

fhimage replied on at Permalink Reply
it is a great discovery to know we can generate pdf thumnail vb.net. i found something here :http://www.rasteredge.com/how-to/vb-net-imaging/pdf-creating/...
Rushing replied on at Permalink Reply
For anyone interested, here's an override for the ImageHelper. Drop it into /helpers/image.php

This overrides the default getThumbnail method to intercept PDF files, and is intended to behave the same.

<?php defined('C5_EXECUTE') or die("Access Denied.");
class ImageHelper extends Concrete5_Helper_Image {
   public function getThumbnail($f, $maxWidth, $maxHeight, $crop=false){
      $fh = Loader::helper('file');
      $filePath = $f->getPath();
      $fileExtension = $fh->getExtension($filePath);
      //Intercept files with PDF extension
      if(strtolower($fileExtension) == 'pdf'){
         $filename = implode('_', array($fileExtension, $f->getFileID(), $f->getFileVersionID(), filemtime($filePath), $maxWidth, $maxHeight, var_export($crop, true))).'.jpg';
         if (!file_exists(DIR_FILES_CACHE . '/' . $filename)) {
            $im = new imagick($filePath.'[0]');
            //Scale and crop the image
            $originalWidth = $im->getImageWidth();
            $originalHeight = $im->getImageHeight();
TeKnoZiz replied on at Permalink Reply
What does this do, exactly? Does the thumbnail appear in the front end when displaying PDF files or does it generate a new thumbnail image in the FileManager you can use?
Rushing replied on at Permalink Reply
It generates a thumbnail to be used on the front end. It is intended to behave the same as the existing getThumbnail method.