views:

1919

answers:

3

Hi all. I am attempting, unsuccessfully, to use Ghostscript to rasterize PDF files with a transparent background to PNG files with a transparent background. I've searched high and low for questions from others attempting the same thing and none of the posted solutions, which as far as I can tell come down to specifying -sDEVICE=pngalpha, have worked with my test files. At this point I would really appreciate any advice or tips a more experienced hand could provide.

My test PDF is located here: http://www.kolossus.com/files/test.pdf

It could be that the issue is with this file, but I doubt it. As far as I can tell, it has no specified background, and when I open the file with a transparency-aware app like Photoshop or Illustrator, sure enough it displays with a transparent background. However, when opened with an application like Adobe Reader the file is rendered with a white background. I believe that this has more to do with the application rendering the PDF than with the PDF itself -- apps like Adobe Reader assume you want to see what a printed document will look like and therefore always show a white canvas behind the artwork -- but I can't be sure.

The gs command I'm using is:

gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -sOutputFile=test.png test.pdf

This produces a PNG that has transparent pixels outside of the bounding box of the artwork in the file, but all pixels that are inside the artwork's bounding box are rasterized against a white background. This is a problem for me, as my artwork has drop shadows and antialiased edges that need to be preserved in the final output, and can't just be postprocessed out with ImageMagick. A sample of my PNG output is at the same location as the pdf above, with .png at the end (stackoverflow won't let me include more than one url in my post).

Interestingly, I see no effects from using the -dBackgroundColor flag, even if I set it to something non-white like -dBackgroundColor=16#ff0000. Perhaps my understanding of the syntax of this flag is wrong.

Also interestingly, I see no effects from using the -dTextAlphaBits=4 -dGraphicsAlphaBits=4 flags to try to enable subpixel antialiasing. I would also appreciate any advice on how to enable subpixel antialiasing, especially on text.

Finally, I'm using GPL Ghostscript 8.64 on Mac OS 10.5.7, and the rendering workflow I'm trying to get set up is to generate transparent PNG images from PDFs output by PrinceXML. I'm calling Ghostscript directly for the rasterization instead of using ImageMagick because ImageMagick delegates to Ghostscript for PDF rasterization and I should be able to control the rasterization better by calling GS directly.

Thanks for your help.

-Jon Wolfe

+1  A: 

Hi, i have the same problem , i tried to use imagemagick but unfortunatelly it is doesn't work for me http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=13462 you may check that may be it will work for you.

Vasilii
Thanks Vasilii. I found that thread before posting here, and nobody responded to my post there. I don't think this is something that can be done with Imagemagick or Ghostscript -- the best you can do is to remove the background manually with imagemagick.
+1  A: 

Afraid I can't tell you what ghostscript can do but I do have a suggestion. Try rendering your PDF with both a black and a white background. Any pixel that comes out the same in the two images was clearly meant to be opaque (i.e., alpha == 1.0). Pixels that are different have a non-zero alpha which can be computed by subtracting the black background pixel from the white background pixel. Give or take some precision, the value of any red, green or blue component will be the alpha value.

George Phillips
+1  A: 

Ghostscript can handle transparency only if at build time the "transpar" option was selected. Assuming your Ghostscript is the right version, you can add a parameter on the commandline:

... -c "0 .setopacityalpha" -f c:/path/to/file/to/be/converted.pdf

(You can use variants for the opacity in the range [0..1]).

Also, have you tried to convert your Ghostscript-created .png (in case that background indeed is white instead of transparent) to a transparent background using ImageMagick's convert or GraphicMagick's gm convert commands? Here is an example:

convert -background transparent test.png test_transp.png

BTW, in case Acrobat or AcroReader show all page backgrounds as white: this is the default setting even for really transparent backgrounds. You can change it in the application's setup options: IIRC the setting was inside the 'Page Display' options and named s.th. like 'Show transparency rasters'. (If I looked it up in detail, it possibly wouldn't help too much -- Adobe keeps shifting all options around in every new release. Just poke around there yourself, you'll find it now that you know it's there.)

pipitas