在 Ruby on Rails 中使用 mini_magick 将 pdf 转换为 png [英] Convert a pdf to png using mini_magick in Ruby on Rails
本文介绍了在 Ruby on Rails 中使用 mini_magick 将 pdf 转换为 png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
背景
我从 API 调用中检索了二进制格式的 pdf:
I retrieved a pdf in binary form from an API call:
base_64_binary_data = @response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
@pdf_file = File.open('label.pdf', 'wb') do |file|
content = Base64.decode64 base_64_binary_data
file << content
end
上面的代码生成了一个我可以查找的文件,并且是我需要的清晰图像.
The above code results in a file that I can look up and is the crystal clear image I need.
问题
我需要将此图像放置在视图中,以便用作发票上的标签.
I need to place this image inside a view in order to function as a label on an invoice.
以下似乎是一个很好的解决方案:
The following seems to be leading to a fine solution:
@pdf = MiniMagick::Image.new(@pdf_file.path)
@pdf.pages.first.write("preview.png")
它在第二行失败.
MiniMagick::Error
`identify label.pdf` 失败,错误:
...第二行...
MiniMagick::Error
`identify label.pdf` failed with error:
...2nd line...
我想以这样的方式工作:
I'd like to work to something like this functioning:
推荐答案
pdf = MiniMagick::Image.open "doc.pdf"
MiniMagick::Tool::Convert.new do |convert|
convert.background "white"
convert.flatten
convert.density 150
convert.quality 100
convert << pdf.pages.first.path
convert << "png8:preview.png"
end
这篇关于在 Ruby on Rails 中使用 mini_magick 将 pdf 转换为 png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文