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 Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.

Navigation
« Double Shot #968 | Main | Double Shot #967 »
Thursday
Sep272012

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=MiniMagick::Image.open("some.pdf") 
image.format("png", 1)
image.resize("200x200")
image.write("some_thumbnail.png")

Or in CarrierWave:


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

  version :web_thumb do
    process :thumbnail_pdf
  end

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

end

Reader Comments (4)

For carrierwave you should be able to do:

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

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):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>