Convert a pdf to png using mini_magick in Ruby on Rails

痞子三分冷 提交于 2019-12-11 03:42:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!