ruby-on-rails - Carrierwave PDF to Image 使用 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 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

但我一直收到错误提示

MiniMagick::Error: Unable to format to png
from /Users/rkamat/.rvm/gems/ruby-1.9.3-p547/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:266:in `format'

在下面这一行

 img.format("png") do |c|

任何关于这方面的帮助将不胜感激,因为我花了几个小时试图消除它。

我已经引用了载波上传中的文档以使其正常工作 https://github.com/carrierwaveuploader/carrierwave/wiki/Efficiently-converting-image-formats

最佳答案

如果你使用 RMagick

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

PDF 是图像列表 ( more info with ImageMagick )。

关于ruby-on-rails - Carrierwave PDF to Image 使用 MiniMagick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968271/

相关文章:

ruby-on-rails - 未知验证器 : 'LenghtValidator' (ArgumentError) when I run RSpec

ruby-on-rails - Rails 错误 - 无法加载此类文件 - aws-sdk(您可能需要安装 aws-sdk gem)

ruby-on-rails - rails : Validate with arguments

css - Nokogiri 并从充满 Nokogiri 节点的数组中隔离选择元素

ruby - Heroku RACK_ENV 在 Thin 上显示 "development",但在 Unicorn 上显示 "staging"

ruby-on-rails - 帮助 collection_select

ruby-on-rails-3 - 为什么 ActionController::Metal 会泄漏内存?

ruby - 带有验证码的表单上的 cucumber 功能

ruby-on-rails - 错误 : Failed to build gem native extension

ruby-on-rails - 错误 NoMethodError:ActionView::Base:Class 的未定义方法 `debug_rjs='