pdf 文件转换成 jpg 文件,有没有相关的教程或文章。
#1 楼 @krazy 与之相比较 rmagick http://stackoverflow.com/questions/65250/convert-a-doc-or-pdf-to-an-image-and-display-a-thumbnail-in-ruby 哪个比较好啊?比如图片清晰度、效率等等。。
@path = "#{Rails.root}/public/b.png"
pdf = Grim::Pdf.new("#{Rails.root}/public/a.pdf")
Grim::ImageMagickProcessor.new.save(pdf,0,@path,{})
生成 png 文件没问题。当生成 b.jpg 时,生成的文件一团黑。
嗯 jpg 不支持透明
这一行改成 "-quality", quality.to_s, "-alpha", "remove", "-colorspace", "RGB",
试试?
其实直接调命令行一样的
修改 grim 源代码 image_magick_processor.rb 添加 "-flatten", "-background", "white",
def save(pdf, index, path, options)
width = options.fetch(:width, Grim::WIDTH)
density = options.fetch(:density, Grim::DENSITY)
quality = options.fetch(:quality, Grim::QUALITY)
command = [@imagemagick_path, "-resize", width.to_s, "-antialias", "-render",
#"-quality", quality.to_s, "-alpha", "remove", "-colorspace", "RGB",
"-quality", quality.to_s, "-colorspace", "RGB",
"-interlace", "none", "-density", density.to_s,
"-flatten", "-background", "white",
"#{Shellwords.shellescape(pdf.path)}[#{index}]", path]
command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path
result = `#{command.join(' ')}`
$? == 0 || raise(UnprocessablePage, result)
end