When converting first page of a PDF into an image using Ghostscript, sometimes I get "extra" space. Why?

tonix

I am building a simple script which converts the first page of a PDF into an image using Ghostscript. Here is the command I use:

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

This works beautifully with some PDFs, e.g. if I convert the first page of a PDF that looks like this:

enter image description here

I actually get this first page as an image and there aren't any problems.

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

enter image description here

With the same gs command, after the conversion, the .png image looks like this:

enter image description here

The problem is that I get this extra white space on the left inside the image when I convert that page, why does GhostsScript do this? Where does that extra blank white space come from?

Kurt Pfeifle

Most likely, your PDFs do not use identical values for /MediaBox and for /CropBox. For details about these technical terms related to a page, see this illustration from the German Wikipedia:

In other words: the /CropBox values (if given) for a PDF page determines which (smaller) part of the overall page information (which is inside the /MediaBox) the PDF viewer should be made visible to the user (or to the printer).

Solution

To determine what are the different values for all the pages of your book(s), run this command:

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

To see these values just for the first page, run

pdfinfo -l 1 -box my.pdf

For Ghostscript to give the results you want, add -dUseCropBox to your command line:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting pdf to image using c# and Ghostscript

From Dev

How to keep margins when converting PDF to EPS outlines using Ghostscript?

From Dev

How to get the first page of a PDF as an image using Carrierwave in rails?

From Dev

Ghostscript error converting multi-page PS to multi-page PDF using pdfwrite

From Dev

Why do I get an extra null terminating character when converting from WCHAR to char?

From Dev

Converting JPG image to PDF without resizing image with ghostscript or itext

From Dev

What settings should I use to keep the colour correct when converting a PDF to x-3:2002 with ghostscript?

From Dev

Receiving invalid filename error when converting first page of pdf to jpg

From Dev

Removing extra white space when using first-letter

From Dev

Splitting a PDF file with Ghostscript results in one extra blank page

From Dev

How to keep page size when converting pdf to image

From Dev

I get an extra input space when I run my code

From Dev

I get an extra input space when I run my code

From Dev

Why is there an extra space when I type "ls" in the Desktop directory?

From Dev

Why do I see extra text at the top of my view page when using the @Model statement?

From Dev

Why do I see extra text at the top of my view page when using the @Model statement?

From Dev

Get rid of extra space in cell when using Text Wrap

From Dev

Converting a PDF to black & white with ghostscript

From Dev

Ghostscript Stamp Image on PDF

From Dev

Why doesn't the second image go under the first when I resize the web page?

From Dev

Why do I get an extra empty hit when using regex to split a string?

From Dev

Why do I get one extra result with null when using ResultSet?

From Dev

First two lines of the first paragraph on each page have an extra space

From Dev

Crop Pdf Page While Converting it to Image

From Dev

How do I insert a blank page into a PDF with ghostscript or pdftk?

From Dev

Cutting pdf page in half and reimposing parts differently using ghostscript

From Dev

Using Ghostscript to convert multi-page PDF into single JPG?

From Dev

How to crop a multipage pdf using ghostscript with an array of page specific cropboxes

From Dev

Print PDF using GhostScript

Related Related

  1. 1

    Converting pdf to image using c# and Ghostscript

  2. 2

    How to keep margins when converting PDF to EPS outlines using Ghostscript?

  3. 3

    How to get the first page of a PDF as an image using Carrierwave in rails?

  4. 4

    Ghostscript error converting multi-page PS to multi-page PDF using pdfwrite

  5. 5

    Why do I get an extra null terminating character when converting from WCHAR to char?

  6. 6

    Converting JPG image to PDF without resizing image with ghostscript or itext

  7. 7

    What settings should I use to keep the colour correct when converting a PDF to x-3:2002 with ghostscript?

  8. 8

    Receiving invalid filename error when converting first page of pdf to jpg

  9. 9

    Removing extra white space when using first-letter

  10. 10

    Splitting a PDF file with Ghostscript results in one extra blank page

  11. 11

    How to keep page size when converting pdf to image

  12. 12

    I get an extra input space when I run my code

  13. 13

    I get an extra input space when I run my code

  14. 14

    Why is there an extra space when I type "ls" in the Desktop directory?

  15. 15

    Why do I see extra text at the top of my view page when using the @Model statement?

  16. 16

    Why do I see extra text at the top of my view page when using the @Model statement?

  17. 17

    Get rid of extra space in cell when using Text Wrap

  18. 18

    Converting a PDF to black & white with ghostscript

  19. 19

    Ghostscript Stamp Image on PDF

  20. 20

    Why doesn't the second image go under the first when I resize the web page?

  21. 21

    Why do I get an extra empty hit when using regex to split a string?

  22. 22

    Why do I get one extra result with null when using ResultSet?

  23. 23

    First two lines of the first paragraph on each page have an extra space

  24. 24

    Crop Pdf Page While Converting it to Image

  25. 25

    How do I insert a blank page into a PDF with ghostscript or pdftk?

  26. 26

    Cutting pdf page in half and reimposing parts differently using ghostscript

  27. 27

    Using Ghostscript to convert multi-page PDF into single JPG?

  28. 28

    How to crop a multipage pdf using ghostscript with an array of page specific cropboxes

  29. 29

    Print PDF using GhostScript

HotTag

Archive