A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time software developer: most of my time in recent years has been spent writing Rails, though I've dabbled in many other things and like most people who have been writing code for decades I can learn new stuff as needed.

Currently I'm employed as the Vice President of Engineering at Faria Education Group. If you're interested in working with me, we're often hiring smart developers. Drop me a comment if you're interested or email MikeG1 [at] larkfarm.com.

« Double Shot #968 | Main | Double Shot #967 »

Thumbnailing PDFs with MiniMagick

Maybe this was already out there somewhere, but my search fu failed. So, for the benefit of anyone else using MiniMagick instead of RMagick and needing thumbnails of PDF files:

image.format("png", 1)

Or in CarrierWave:

class PdfAssetUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  version :web_thumb do
    process :thumbnail_pdf

  def thumbnail_pdf
    manipulate! do |img|
      img.format("png", 1)
      img = yield(img) if block_given?


Reader Comments (4)

For carrierwave you should be able to do:

version :small do
process resize_to_fill: [32, 32]

There are a variety of processes & resizes

September 27, 2012 | Unregistered CommenterAnkur

Yes, there are. But with a MiniMagick backend and PDF uploads, they don't do the job.

September 27, 2012 | Registered CommenterMike Gunderloy

This is great, but it kept failing on my mac system until i did 'brew install ghostscript' then it worked like a charm, though it takes about 8 seconds to scale a 500kb pdf...

November 1, 2012 | Unregistered Commenterwayne

I just spent a long while trying to figure out how to exactly this with Carrierwave and Rmagick. Eventually switched to MiniMagick because of Rmagick memory usage on the server. This solution is way cleaner then what I came up with, but I didn't have luck with it.

To convert the first page of a pdf to thumbnail I came up with this code https://gist.github.com/4567494

I couldn't get the mime-type working, the whole thing feels hackish, would love to get your thoughts.

January 18, 2013 | Unregistered CommenterRyan Haywood

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>