Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first page of a PDF as an image using Carrierwave in rails?

Imagemagick, and the mini_magick gem are both installed, yet I cannot get the model to save when I upload a pdf.

Upon trying to create a new instance of the model, I get the following error:

Pdf Failed to manipulate with MiniMagick, maybe it is not an image?

What am I doing wrong here? My intention was to use the solution shown at: http://afreshcup.com/home/2012/9/27/thumbnailing-pdfs-with-minimagick.html

My uploader:

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

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

  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
like image 558
croceldon Avatar asked Nov 02 '15 21:11

croceldon


1 Answers

Did you install ghostscript?

brew install ghostscript
like image 53
Yury Lebedev Avatar answered Oct 29 '22 14:10

Yury Lebedev