Convert PDF to PNG with transparent background

We have a Ruby on Rails application that should convert PDF to PNG with a transparent background. We are using rmagick 2.13.1. On our development machines, the following code works exactly the way we want it.

pages = Magick::Image.from_blob(book.to_pdf.render){ self.density = 300 } page = pages[0] image_file = Tempfile.new(['preview_image', '.png']) image_file.binmode image_file.write( page.to_blob { |opt| opt.format = "PNG" } ) 

Then we save the image_file file, and all that is peach. However, when you deploy the verification server to Heroku on the server, the generated image has a white background. It turns out that Heroku cedar cedar uses imagemagick ImageMagick 6.5.7-8 2010-12-02, where we use ImageMagick 6.7.5-7 2012-05-08.

I scanned the web for older posts that might apply to the old version to try to figure out how to create transparent PNGs. It was certainly supported, but so far I have not been able to find the right combination of settings.

To make sure this was not a problem in the PDF format, I uploaded the PDF file generated by Heroku and successfully converted it using the above code (slightly modified to read the file instead) into transparent PNG.

Some of the things I've tried in various combinations are:

  • page.matte = true
  • page.format = "PNG32"
  • page.background_color = "none"
  • page.transparent_color = "white"
  • page.transparent("white")

So the question is, is this possible? If so, what settings do I need to set on the image before recording it?

I am also investigating, including the compiled binary of the more modern Imagemagick on Heroku.

Any help is appreciated.

+4
source share

All Articles