问题
Background
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.
Issue
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")
It fails on the second line.
MiniMagick::Error
`identify label.pdf` failed with error:
...2nd line...
I'd like to work to something like this functioning:
回答1:
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
来源:https://stackoverflow.com/questions/45937501/convert-a-pdf-to-png-using-mini-magick-in-ruby-on-rails