PDF to PNG Conversion Removes Margins

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

PDF to PNG Conversion Removes Margins

Post by TangoOversway »

I've tried this:

convert -background transparent -density 150x150 -page Letter Background.pdf[0] BackgroundParts-00-150DPI.png

I've done it with the -density option set, and with -page set, then without the -density and without the -page, and with only one of each (in other words all 4 combinations of using or not using those arguments). My problem is that the resulting PNG file is always cropped to fit exactly around the text area or any images. There is no margin at all, so when I pull the image up in a reader, it looks a little sloppy with lines starting at exactly the left edge or some ending on the right edge. It makes it feel odd to read.

How can I tell convert to include the entire 8.5" x 11" page in the PNG file - including margins or borders?


ADDITIONAL INFORMATION: I opened one of my test PNGs in GIMP to check it out (I had been using only Preview (in OS X) before this). In GIMP, I see that there is a lot of space that's transparent and is white in Preview, but I also see (when I zoom to fit) that the margins are apparently there, just not visible.

Is there more than one type of transparency in an image? Why is there this space around the text that's transparent and not white when the transparent area within the normal page is white?

I'd upload images, but I don't see a way to do that here. I see the img tag button, but no way to upload.
Last edited by TangoOversway on 2012-12-28T13:08:19-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

Look up the page size in pixels from the table at http://www.imagemagick.org/script/comma ... s.php#page and use -extent with that size rather than -page. Also read the input after the -density and the rest of the options afterwards

convert -density 150x150 Background.pdf[0] -gravity center -background transparent -extent 612x792 BackgroundParts-00-150DPI.png
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

Thank you. I may have misunderstood the docs, but there's a problem - when I try this, I'm getting only a section of the page.

I tried this and it gave me a picture of only part of the page. I changed the 612x792 to 1275x1650 (got those numbers from multiplying 150 DPI by 8.5 and 11 inches for the size), and that gave me a much larger image, but still without the borders or margins. I have to get to a class right now, but when I'm back, I'll check on the other link and go over that to see what else I can try.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

I think it would be best to provide a link to your input pdf and output png and explain what margins you mean. Do you want some kind of white border around the pdf when expanded to the page size?
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

Here's the images. These are screenshots of the PDF or PNGs in Preview (the default app for viewing almost anything on OS X). In each case, I set the zoom setting to "fit to window," so I'm assuming the actual image size in all of them is the same size as the one in the first image - but that the margin or boarder is transparent.

This is the PDF File:

http://halblog.com/binaries/1-Source.png

This is the PNG file I get when using the command line I gave for my first post:
(Command used: convert -background transparent -density 150x150 -page Letter Background.pdf[0] BackgroundParts-00-150DPI.png)

http://halblog.com/binaries/2-Regular.png

This is the PNG file I got with the other command line:
(Command used: convert -density 150x150 Background.pdf[0] -gravity center -background transparent -extent 612x792 BackgroundParts-00-150DPI.png)
http://halblog.com/binaries/3-Modified.png

This is the PNG file I got by modifying the previous command line by changing the -extent to numbers that I got from multiplying the page by 150DPI:
(Command used: convert -density 150x150 Background.pdf[0] -gravity center -background transparent -extent 1275x1650 BackgroundParts-00-150DPI.png)
http://halblog.com/binaries/4-ModifiedBig.png

And this is when I used Ghostscript, then ImageMagick. I have to find the two commands I used, but I'm including this now to show how this image does seem to have a transparent area that is seen as white normally, but is dark in this image.
http://halblog.com/binaries/5-FromGS2IM.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

try using -background white rather than -background transparent.

If that does not do what you want, the post a link to the actual pdf rather than a screen shot as we need to review what is actually in the pdf.
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

It didn't work, so here's the link to the PDF:

http://halblog.com/binaries/0-OriginalFile.pdf

Anything else I can provide, let me know, since this looks like it's weird enough it could effect other things, too.

ADDED:

Oh, and it was produced by LibreOffice - if that matters.
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

I got it to almost work. This command line:

Code: Select all

convert MamboTest.pdf -gravity center -background white -extent 612x792 MamboTest-PageSet.png
Gave me margins and a border, but it's not exactly the same size, in a Preview window, as the PDF, but that could be for several reasons. However, it is only 72 DPI. Here's the image:

http://halblog.com/binaries/6-BordersLowRes.png

I tried replacing 612x792 with 1275x1650, seeing if that would force a higher DPI, but it gave me the bigger canvas with the image much smaller in the center. Then I tried setting "-density 150x150." Without using -page or -extent, that just gives me the page without margins. But if I use "-density 150x150 -extent 612x792" I get a lower res image again. (I think the -extent is overriding the -density setting.) If I use "-density 150x150 -extent 1275x1650" I get the large canvas with the small image again. It seems -extent overrides -density (or maybe the last one given sticks).

If I could get the image above with a 150 DPI, I'd be so happy I'd jump up and do a cha-cha.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

PDFs are vector files and so have no size. You need to specify an appropriate density before reading the PDF to get the result to the pixel size you want before extending into the letter page size. You will have to use trial and error. Nominally if you leave off the -density before the PDF, I believe it will assume 72 dpi. If you want the output png to be some specific density, then add -density again before writing the output.

convert -density XX MamboTest.pdf -gravity center -background white -extent 612x792 -density YY MamboTest-PageSet.png
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

Okay, I'm playing around with that now. I had some issue at one point - with all the experimentation, it's sometimes hard to keep it all straight. For a while I couldn't get -density to behave.

Do -extent -page ever "fight" with -density? It seems like using -extent can force it to ignore -density 150x150 and force it back to a default 72x72.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

TangoOversway wrote:Okay, I'm playing around with that now. I had some issue at one point - with all the experimentation, it's sometimes hard to keep it all straight. For a while I couldn't get -density to behave.

Do -extent -page ever "fight" with -density? It seems like using -extent can force it to ignore -density 150x150 and force it back to a default 72x72.

I believe that you need to specify two densities, one for reading the PDF and a second to set the dpi for the png. But PNG only supports dots per centimeter, so if you set the density for the png, you need to use -units to tell it that you are setting them to inches (or centimeters) and IM should convert inches to the corresponding dots per centimeter to correspond.

see
http://www.imagemagick.org/script/comma ... hp#density
http://www.imagemagick.org/script/comma ... .php#units
TangoOversway
Posts: 7
Joined: 2012-12-28T00:06:04-07:00
Authentication code: 6789

Re: PDF to PNG Conversion Removes Margins

Post by TangoOversway »

First, fmw42, thank you for all the patience and help!

It's working.

I was checking my earlier files and found that -density 150x150 wasn't working - but you're comment about using -units got it to work.

Here's the command that did it:

Code: Select all

convert -density 150x150 MamboTest.pdf -background white -units PixelsPerInch -density 150 -extent 1275x1650 MamboTest-PageSet.png
I used 150DPI and multiplied 150 by 8.5 for the width and by 11 for the height and used that for -extent (as I mentioned earlier).

This makes it easy for me to do all the adjusting I need. For instance, if the page in a PDF is landscape, I can change the dimensions accordingly and I can easily multiply the density by the width and height. (Eventually I'll be calling the program from Java - I'm not using any interface, just calling it with the one command.)

I was experimenting with the density settings - it seems 150 works the same as 150x150. Am I right about that?

Also, have you had a chance to check the PDF? I've seen many posts, here and elsewhere, about people wanting to reduce the margins on a PDF -> PNG conversino, but none about wanting to preserve the margins. I'm wondering if that's because of my unfamiliarity with ImageMagick or if it's an unusual situation.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG Conversion Removes Margins

Post by fmw42 »

TangoOversway wrote: I was experimenting with the density settings - it seems 150 works the same as 150x150. Am I right about that?

Also, have you had a chance to check the PDF? I've seen many posts, here and elsewhere, about people wanting to reduce the margins on a PDF -> PNG conversino, but none about wanting to preserve the margins. I'm wondering if that's because of my unfamiliarity with ImageMagick or if it's an unusual situation.

Yes, 150 defaults to 150 in both dimensions, so it is the same as 150x150.

I have not looked at your PDF, since it sounded like you were on your way to getting the results you wanted. I think the margin issue is not special. But perhaps what you have posted will help others as well. I think the main thing was using two densities and using -extent.

The -page letter works also, but it does not allow for the centering. As I understand it -page letter is like -page 612x792+0+0. That is the offsets are fixed at 0,0 relative to the top left corner. Otherwise a simpler command would have worked such as -page letter -background white -flatten. But -flatten is not gravity sensitive.
Post Reply