Convert a pdf to png using mini_magick in Ruby on

2019-07-19 00:43发布

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条回答
Luminary・发光体
2楼-- · 2019-07-19 01:45
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
查看更多
登录 后发表回答