When converting the first PDF page to an image using Ghostscript, I sometimes get "extra" space. What for?

I am creating a simple script that converts the first PDF page to an image using Ghostscript. Here is the command I'm using:

gs -q -o output.png -sDEVICE=pngalpha -dLastPage=1 input.pdf 

      

This works great with some PDFs, for example. if I convert the first PDF page that looks like this:

enter image description here

I actually get this first page as an image and no problem.

But I noticed that with some first pages of other PDFs, like the following:

enter image description here

With the same command gs

after conversion, the .png image looks like this:

enter image description here

The problem is I am getting this extra free space on the left inside the image when I convert this page, why is GhostsScript doing this? Where does this extra white space come from?

+3


source to share


1 answer


Chances are your PDFs don't use the same values ​​for /MediaBox

and for /CropBox

. For more on these technical terms associated with the page, see this illustration from the German Wikipedia :

400px-PDF_BOX_01.svg.png

In other words: the values /CropBox

(if given) for the PDF page determine which (smaller) piece of general information about the page (which is inside /MediaBox

) the PDF viewer should be visible to the user (or to the printer).

Decision

To determine what the different values ​​are for all pages in your book, run the following command:



pdfinfo -f 1 -l 1000 -box my.pdf

      

To see these values ​​for the first page only, run

pdfinfo -l 1 -box my.pdf

      

To get Ghostscript to produce the results you want, add -dUseCropBox

to your command line:

gs -q -o output.png -sDEVICE=pngalpha -dLastPage=1 -dUseCropBox input.pdf 

      

+5


source







All Articles