将PDF转换为具有透明背景的PNG [英] Converting PDF to PNG with transparent background
问题描述
我们有一个Ruby on Rails应用程序,需要将PDF转换为具有透明背景的PNG.我们正在使用rmagick 2.13.1.在我们的开发机器上,以下代码可以完全按照我们的要求工作.
We have a Ruby on Rails application that needs to convert a PDF into a PNG with a transparent background. We're using rmagick 2.13.1. On our development machines the following code works exactly how 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" } )
然后我们保存image_file,所有内容都是桃红色的.但是,当我们部署到Heroku上的评论服务器时,生成的图像具有白色背景.事实证明,Heroku的雪松堆栈使用的是imagemagick ImageMagick 6.5.7-8 2010-12-02,我们在开发机器上使用的是ImageMagick 6.7.5-7 2012-05-08.
We thens save the image_file and all is peachy. When we deployed to a review server on Heroku, though, the generated image has a white background. It turns out that Heroku's cedar stack is using imagemagick ImageMagick 6.5.7-8 2010-12-02 where we're using ImageMagick 6.7.5-7 2012-05-08 on our development machines.
我已经在网上搜寻了较旧的帖子,这些帖子可能适用于较旧的版本,以尝试弄清楚如何生成透明的PNG.它肯定受支持,但是到目前为止,我还无法弄清楚设置的正确组合.
I've scoured the net for older posts that might apply to the older version to try and figure out how to generate the transparent PNGs. It's surely supported, but, so far I haven't been able to figure out the right combination of settings.
为验证不是问题的是PDF生成,我下载了在Heroku上生成的PDF,并使用上述代码(将其稍作修改以读取文件而不是生成文件)成功地将其转换为透明的PNG
To verify that it wasn't the PDF generation that was the problem, I downloaded a PDF generated on Heroku and successfully converted it using the above code (slightly modified to read the file in instead of generate it) to a 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")
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, which settings do I need to set on the image before writing it out?
我还正在研究在Heroku上包含更新的Imagemagick的已编译二进制文件.
I'm also investigating including a compiled binary of a more up to date Imagemagick on Heroku.
感谢您的帮助.
推荐答案
这不再是问题,因为Heroku在其各个堆栈上都有ImageMagick 6.7-6.9版本.
This should no longer be an issue, as Heroku has ImageMagick versions 6.7-6.9 on their various stacks.
这篇关于将PDF转换为具有透明背景的PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!