Minimagick gem fails to convert multipage PDF to several png images

社会主义新天地 提交于 2019-12-25 02:58:35

问题


I am new to RoR and I'm trying to develop a slide sharing app through which users can upload and share a multipage PDFs containing a deck of slides (one slide per page). Viewers can then view these slides on a "show" page.

Behind the scenes I am trying to convert the multipage pdf into several .png files (1 pdf page -> 1.png file), and then display them in a carousel/slider fashion.

Here is my uploader:

# encoding: utf-8

class DocUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
  include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
  storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
  #  'public/doc_uploads/'
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  process :filename
  process :generate_png

  def generate_png
    pdf = MiniMagick::Image.new(self.file.path)
    pdf.pages.each_with_index do |page, index|
      page.write("page#{index}.png")
    end
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
    def extension_white_list
      %w(pdf)
    end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  def filename
    'doc.pdf' if original_filename
  end

end

When I try to upload a multipage pdf document, it throws me this error: 'identify /public/uploads/tmp/1443619784-406-3861/doc.pdf' failed with error: at the line pdf.pages.each_with_index do |page, index|

I have tried different solutions to converting the document, but none of them have worked for me so far. Why would a run of the mill .pdf file fail minimagick's validation? Is there anything I can do to avoid running into this issue? Any advice will be greatly appreciated!

来源:https://stackoverflow.com/questions/32868326/minimagick-gem-fails-to-convert-multipage-pdf-to-several-png-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!