简体   繁体   English

Imagemagick如何将具有透明度的PDF转换为PNG

[英]Imagemagick How to Convert PDF with transparency to PNG

I have a project that has makes a PDF for print. 我有一个项目,该项目可打印PDF。 I then have it converted to PNG to show back to the client, but the PNG is not retaining the transparency that is in the PDF. 然后,我将其转换为PNG以显示回客户端,但是PNG并未保留PDF中的透明度。 I checked the PDF by opening it up in Photoshop and the transparency exists, but it's not converting this data over to the PNG. 我通过在Photoshop中打开PDF进行了检查,并且存在透明度,但是它没有将这些数据转换为PNG。

The PDF I am using is at: http://mattodesigns.com/PDFs/PNG/testing2.pdf 我使用的PDF位于: http : //mattodesigns.com/PDFs/PNG/testing2.pdf

The page that I am running the conversion on is at: http://mattodesigns.com/PDFs/PNG/pdf2png.html 我正在运行转换的页面位于: http : //mattodesigns.com/PDFs/PNG/pdf2png.html

The PHP Code that I am using to convert the PDF to PNG is: 我用来将PDF转换为PNG的PHP代码是:

<?php
$source = 'http://mattodesigns.com/PDFs/PNG/testing2.pdf';
$target = 'testing2.png';

//PNG preview for front
$imagefront = new Imagick();
$imagefront->setResolution( 150, 150 );
$imagefront->readimage($source);
$imagefront->setImageFormat( "PNG32" );
$imagefront->writeImage($target);
?>

This is not working and not sure how I get this to retain the transparency that is in the PDF.I have been looking all over for a solution that is not via a command line and haven't been able to find out. 这是行不通的,并且不确定如何保持PDF的透明性。我一直在寻找一个不是通过命令行的解决方案,也一直无法找到解决方案。 I have also been trying all sorts of various combonation of Imagemagick settings with no luck. 我也一直在尝试Imagemagick设置的各种组合,但是没有运气。

If I need to make a mask, I can do that, but would rather not do that if I don't have to, as all the data is in the PDF itself. 如果需要遮罩,则可以这样做,但如果不需要,则不要这样做,因为所有数据都在PDF本身中。

For those who have the same issue, I ended up using GhostScript, following the answer on this topic : http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=14546&start=15 对于那些有相同问题的人,我最终按照以下主题的答案使用了GhostScript: http : //www.imagemagick.org/discourse-server/viewtopic.php? f=1&t=14546&start =15

exec("gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -dFirstPage=1 -dLastPage=1 -sOutputFile=output.png input.pdf");

It looks like ImageMagick is having problems handling semi-transparent PNGs, and GhostScript has much better results. 看起来ImageMagick在处理半透明的PNG时遇到问题,GhostScript的结果要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM