1
votes

I'm using ImageMagick to convert PDF files to PNGs. (Lots of text, so I'd rather use PNG over JPEG.) I'm doing this on OS X, 10.8.2.

I tried using Ghostscript, then ImageMagick, as was done here and I got a gray background. I shortened it to one line, since ImageMagick can work with PDFs and tried this:

 convert -background transparent -transparent white \
          Background-Page01.pdf TestClearX1.png

I've tried that with PNG files and JPEG files. (And with and without -transparent white as well.)

If it's a JPEG, I can get a white background (which is probably clear, but in my viewer, I can't tell), but with a PNG, I always get a dark background. I thought about, as a test, trying to generate a BMP, then converting that to PNG, but it won't generate BMP files.

How can I get a transparent background for a PNG file?

And if that's not possible, since JPEG files are not good for text, is there a better alternative?

2
Does the PDF itself have a transparent background? Also, are these multipage PDFs?PinnyM
Single page PDFs. They appear to be transparent backgrounds. The ones I'm using for testing are mostly text (I have one with images, which I'll use after I get text only working). I generated them from LibreOffice. I created a normal text document - no colors used in anything (black letters on the normal white background). I'm using a 4 page document and a 94 page document. I have generated 1 page PDFs of all 4 pages of the first document and a few pages of the second one.Tango
Just tested this; assuming the PDFs have transparent backgrounds convert foo.pdf bar.png should work just fine to produce transparent PNGs. Also tested convert -transparent white foo.pdf bar.png (with white background in PDF) and got correct results. If you're still having issues make sure your ImageMagick install is up to date and you don't have wonky PDFs.Matt
@Matt: Any idea how I can verify the PDF is good, or has an appropriate background? They're generated by LibreOffice.Tango
Er. Try re-saving the PDF. Open the PDF in another program and save it again (if you're on a Mac, Preview would work, if you have Adobe Acrobat Pro or Illustrator these will work anywhere). On another note, just to cover all the bases, you say the PNG is "dark"—please be sure that's not the background showing through :) Can you post an example PNG?Matt

2 Answers

1
votes

There's two ways to export a PDF in LibreOffice, and one way does not provide any warnings. The other way provides a warning that PDF/A cannot have transparent objects. The problem is you're using PDF/A files that don't support transparent objects—this results in your PNG always having a background colour.

2
votes

This isn't an answer, but the format of the comment section doesn't allow sensible formatting, so I am offering it here instead.

You alluded to the fact that Preview in OS X doesn't show transparency properly, and that is correct. To get around that, I made the following script, which overlays the files you want to view onto a checkerboard pattern like Photoshop does. I save it as preview and then use chmod +x preview on it to make it executable. Here is the script:

#!/bin/bash
################################################################################
# preview
# Preview images using OSX "open" but overlay images on top of checkerboard 
# first so you can see transparency/transparent pixels, since Preview renders
# them grey :-(
################################################################################
for f in "$@"
do
   base=$(basename "$f")
   composite -compose Dst_Over -tile pattern:checkerboard "$f" "/tmp/$base"
   open -g "/tmp/$base"
done

That enables you to do:

/.preview file1.png file2.gif

and the -g option also means Preview doesn't steal focus too :-)

So, instead of looking like this:

enter image description here

it looks like this:

enter image description here