Convert a pdf to png using mini_magick in Ruby on Rails
Asked Answered
C

1

5

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:

Carrollcarronade answered 29/8, 2017 at 11:4 Comment(0)
M
7
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
Meunier answered 25/3, 2018 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.