I was just trying to figure out how to get a thumbnail preview of PDF and PS files. Apparently there's no support for that, so I tweaked some of the exisiting code for a quick solution. This is pretty straight forward, how I find, though it is still dirty, 'cause it changes modules of other people. But perhaps someone likes to use it as a workaround until some similar solution makes it into the original module code.

Ok, let's patch imagecache.module:

$dst = imagecache_create_path($preset['presetname'], $path);
+  if ($gs = in_array(strtolower(substr($dst, strrpos($dst, "."))), array(".ps", ".pdf"))) $dst .= ".png";

This is for telling the ImageAPI to produce a PNG output as a thumb, not a small PDF.

-   if (!getimagesize($src)) {
+   if (!getimagesize($src) && !$gs) {

This is for making imagecache continue, even without a bitmap style image.

That's it with the in-module code.
Now add a new imagecache preset through your drupal site's admin menu and, as the only action to take, choose custom action and enter this:

_imageapi_imagemagick_convert($image->source."[0]", $image->source.=".png", array(0 => "-thumbnail 100"));
return $image;

The 100 can be replaced by other sizes or even by fixed values like 120x80, the [0] stands for the first page of the document. Just have a look at man convert.

You should be able to add more converting actions to - for example - put a frame and the PDF-Logo or something. The calculated preset is available in Views, just where you want it.

Don't forget to change your ImageAPI to Imagemagick and have GhostScript installed.
Sure.. this is not perfect, but it's a quick solution that - hopefully - will not require many changes once this kind of functionality becomes part of ImageCache in a more professional way.

So, if someone can make use of this.. just let me know.

Comments

WorldFallz’s picture

Thanks for writing this up! You should really consider submitting this as a patch to the imagecache module.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

develCuy’s picture

Just attached a patch mentioning this post at #366373: PDF support ( i.e. convert PDF to JPG support)

--
A drupalero latino,
Working at Dilygent and
Blogging at http://develCuy.com

KG2’s picture

This is fantastic! Just what I needed - thanks for posting, frankiedesign!

Just one question - the first time I click on the new pdf thumbnails I get the following warning:

warning: extract() [function.extract]: First argument should be an array in ...\sites\all\modules\imagecache_actions\imagecache_customactions.module on line 121.

Could anyone tell me what this means, how to fix it, or if it's serious?

Thanks again

vthirteen’s picture

is there a way to make it work with ImageGD instead of ImageMagick?
thank you!

cbrody’s picture

Hi,

Thanks for this -- it would be exactly what I need if I could get it to work! I've installed all the necessary binaries and modules, added a file field to the relevant content type with the display set to my imagecache preset, but the conversion isn't happening. The resultant html contains the correct link to the pdf file but the img src is set to the pdf filename in the imagecache directory instead of an image file. No image is being created. Perhaps I've missed a step somewhere?

Update: I've found a solution that (almost) works: http://drupal.org/node/366373

rachel_norfolk’s picture

This looks fantastic and I plan to have a bit of a play with this tomorrow for a future project...

One question comes to mind now - I would really like to take this a step forward and create images of all the pages in the PDF and attach them to the node. Have you already tried to do this??

Cheers!!

rich_lang’s picture

Hi,

Your instructions were perfect and I have my site generating a png file from the pdf that I upload via filefield.
But...how do I go about getting the page to display both the image and the original link to the file. There is no option in the display fields to do this :-(

Thanks
Rich

jeno’s picture

Few quick question tho. Does it work with multiple pages PDF? PDF with dynamic content and java script such (as a pdf form)?

Thanks

Yuki’s picture

subscribe

pyrello’s picture

subscribe

Sean

anrikun’s picture

See http://drupal.org/node/578804
If you're interested I can post the code.

pyrello’s picture

I am interested!

Sean

anrikun’s picture

ShannonK’s picture

subscribe

tomdisher’s picture

subscribing.

grincon’s picture

westbywest’s picture

I'm curious if anyone has made an attempt to get this working under D7. It looks like things have been muddled with transition of imageapi / imagecache into core, and the complete disappearance of the function imagecache_create_path().

Ben West

eewing’s picture

I'm also working in D7 and need a streamlined way for non-technical front-end users to add PDF newsletters with linked thumbnails. Seems like this must be out there for D7 already, I'm just not finding it.

westbywest’s picture

I was finally able to do this, although it involved adding lots of custom code to theme templates that invoke functions from the imagemagick module, and generate thumbnails of PDFs on demand. The only way I can imagine getting this better integrated into the D7 media module is to push this code into a custom module that provides additional style(s) to the media module based on MIME type.

Here is example code from API.txt provided with the media module. Basically, a custom module would invoke the PDF/EPS thumbnail generation when the relevant MIME type is encountered.

function hook_media_styles($mimetype = NULL) {
  switch ($mimetype) {
    case 'video/x-youtube':
      return array(
        'youtube_video' => t('YouTube Video'),
        'youtube_thumbnail' => t('YouTube Thumbnail'),
        'youtube_shadowbox' => t('YouTube Shadowbox'),
        'youtube_link' => t('Youtube (link to original)'),
    case 'image/x-flickr':
      $styles = array(
        'flickr__original' => t('Flickr (Original size)'),
      );
      foreach (image_styles() as $style_name => $style) {
        $styles[$style_name] = t('Flickr (@style)', array('@style' => $style->name));
      }
      return $styles;
  }
}

Ben West

beauz’s picture

I'd love to see something like this for D7 too...