简体   繁体   中英

Imagemagick How to Convert PDF with transparency to PNG

I have a project that has makes a PDF for print. 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. 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.

The PDF I am using is at: 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

The PHP Code that I am using to convert the PDF to PNG is:

<?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. I have also been trying all sorts of various combonation of Imagemagick settings with no luck.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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