PDF to JPG causes tons of white space and random placement?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
scande3
Posts: 2
Joined: 2016-04-22T19:17:43-07:00
Authentication code: 1151

PDF to JPG causes tons of white space and random placement?

Post by scande3 »

I have several PDF files that seem to react quite differently when converted to a jpg. Essentially: they all generate with a ton of padded white space with the image placed randomly on the white canvas. Opening these PDFs do not display anything but the images centered. I have two samples of this behavior:

Using http://www.phdgaming.com/temp/pdf1.pdf, I get http://www.phdgaming.com/temp/image1.jpg that has the image in the top left of a large white canvas. The most basic command run for this (as more complex variations all failed) is:

Code: Select all

convert pdf1.pdf image1.jpg
Using http://www.phdgaming.com/temp/pdf2.pdf, I get http://www.phdgaming.com/temp/image2.jpg that has the image in the bottom right of a large white canvas. The most basic command run for this (as more complex variations all failed) is:

Code: Select all

convert pdf2.pdf image2.jpg
There are six total PDFs that are showing this behavior that I can't find a solution to after a few hours of searching. I'd appreciate any guidance that might help with this. (From what might be incorrect about the source PDFs, to how I could get the image to generate in the same place of the jpg, to how I could crop out the white background programmatically). Thanks in advance!

Oh - and my Image Magick version information:

Code: Select all

Version: ImageMagick 6.9.3-7 Q16 x86_64 2016-03-14 http://www.imagemagick.org
Delegates (built-in): bzlib djvu fontconfig freetype gvc jbig jng jp2 jpeg lcms lqr lzma openexr png tiff wmf x xml zlib
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to JPG causes tons of white space and random placement?

Post by fmw42 »

What platform? Your Delegates does not show gs or gslib for Ghostscript, which is needed for converting PDF to a raster format. Not showing does not always mean it is not there. But you need to check to see if your version is current.

Your white padding is coming from the PDF. You likely have a raster image imbedded in a larger vector PDF shell. I am not sure how to get rid of that, except to extract the raster file from the pdf using something like pdfimage. See http://www.imagemagick.org/Usage/formats/#ps

On unix you can often get the GS version from

Code: Select all

gs --version
9.16 (mine, but it is not the most current)

See if you can find your version. You may need to upgrade your version of Ghostscript.

Alternately, you can trim the image after converting it.

Code: Select all

convert pdf1.pdf -trim +repage result.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF to JPG causes tons of white space and random placement?

Post by snibgo »

You should also give your version of Ghostscript.

The images don't have a white canvas, but a transparent canvas. (Jpg can't record transparency.) We can trim off the transparency:

Code: Select all

convert pdf1.pdf -trim +repage out.png
Alternatively, you can extract the embedded raster image with pdfimages.

(Cross-posted with Fred.)
snibgo's IM pages: im.snibgo.com
scande3
Posts: 2
Joined: 2016-04-22T19:17:43-07:00
Authentication code: 1151

Re: PDF to JPG causes tons of white space and random placement?

Post by scande3 »

@fmw42 -> Using "-trim +repage" seems to remove the excessive space so that seems to solve my issue in this case. Not sure how the PDF has a transparent background (it never seems to show in any viewer) but these are files that I'm not sure what process created them. Thank you for the help!

@snibgo -> Thanks for your follow-up reply too!

As a late update that isn't that relevant if I use the command provided, my gs --version was 9.16.
Post Reply