Ruby on rails 使用MiniMagick将PDF文件传送到图像

Ruby on rails 使用MiniMagick将PDF文件传送到图像,ruby-on-rails,ruby,ruby-on-rails-3,carrierwave,minimagick,Ruby On Rails,Ruby,Ruby On Rails 3,Carrierwave,Minimagick,我的要求是,如果上传pdf文件,则将pdf转换为图像。到目前为止,这就是我所做的 class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick include CarrierWave::MimeTypes # Choose

我的要求是,如果上传pdf文件,则将pdf转换为图像。到目前为止,这就是我所做的

class ImageUploader < CarrierWave::Uploader::Base

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

  # 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
    "#{PRIVATE_UPLOADS_PATH}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  process :set_content_type
  process :set_model_ext_attributes

  # Create different versions of your uploaded files:
  version :large, if: :image? do
    process :resize_and_pad => [800, 600]
  end
  version :thumb, if: :image? do
    process :resize_and_pad => [100, 100]
  end

  version :normal, if: :pdf? do
    process :efficient_conversion => [640, 960]
  end

  def efficient_conversion(width, height)
    manipulate! do |img|
      img.format("png") do |c|
        c.fuzz        "3%"
        c.trim
        c.resize      "#{width}x#{height}>"
        c.resize      "#{width}x#{height}<"
      end
      img
    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(jpg jpeg gif png pdf)
  end

  def set_model_ext_attributes
    model.display_filename ||= file.filename
    model.content_type_cd = CONTENT_TYPES.rassoc(file.content_type).first if file.content_type
    model.file_size = file.size
  end

  def content_type
    CONTENT_TYPES.assoc(model.content_type_cd).last
  end

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

  protected
    def image?(new_file)
      new_file.content_type.start_with? 'image/'
    end

    def pdf?(new_file)
      new_file.content_type.end_with? '/pdf'
    end

end
在下面这一行

 img.format("png") do |c|
任何有关这方面的帮助将不胜感激,因为我花了数小时试图揭穿这一点

如果您使用RMagick,我让veen参考carrierwave上传中的文档以使其正常工作

require 'RMagick'
pdf_file = Magick::ImageList.new('your_file.pdf')

PDF是图像列表()。

我使用的是minimagick,而不是Rmagick。不能使用MInimagick转换pdf吗?是的,这是可能的,但我从未测试过。我用Minimagik找到了。是的,我尝试了那篇文章中的代码,结果得到了与我在描述中发布的相同的错误。我必须在我的mac上安装Ghostscript才能让它工作。然后我就用操纵!让我的pdf呈现。我不得不一页一页地循环输入pdf文件,以将每个pdf页面转换为图像。我还尝试通过安装rmagick来使用您的解决方案,这对您来说效果很好。谢谢你的帮助。安装
gs
也是我的解决方案,非常感谢:D
require 'RMagick'
pdf_file = Magick::ImageList.new('your_file.pdf')