2

I am new to ghostscript.

I have a pdf which contains a card. i want to crop that card out.

Currently with the understanding of document i am only able to convert the pdf to image but have no luck in cropping. Saw every other related question but there are not working for me.

This is code i used in batch file for converting the pdf to image:

"C:\Program Files\gs\gs9.50\bin\gswin64c.exe" -sDEVICE=png16m -r300 -o c:\users\jen\desktop\pdf.png -f "c:\users\jen\desktop\pdf.pdf
pause 

now i don't know how to crop with it too ? i want to crop at certain postition like: Left:28 Top:524 Width:492.3 Height:161

EDIT I will be using this in firebase functions.

Example PDF file THE_PDF_TO_CROP. I want to cutout the blue area of pdf to image.

jecol
  • 21
  • 7

1 Answers1

1

You need to set several parameters; Firstly you need to specify the width and height of the output bitmap. You can use either -dDEVICEHEIGHTPOINTS and -dDEVICEWIDTHPOINTS, or alternatively you can specify the output size in pixels using -g<x>x<y> where and are the number of pixels in the x and y directions. Obviously that will vary depending on the resolution. You can't (obviously) use fractional pixels.

If you use -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS then you also need to set -dFIXEDMEDIA to tell the interpreter not to use the media size from the PDF file instead.

So that shoould create an output bitmap of the correct size. If you try rendering your file using just that, you will see that it renders just a portion of the page from the bottom left. So now you need to shift the content around so that the portion you want lies at the bottom left of the media. You can do that by using the PageOffset PostScript operator.

You haven't given any numbers, nor supplied an example file, so lets say (for the sake of example) that you want to render a 1 inch by 2 inch portion of the document. Lets further say that you the part you want rendered starts 2.5 inches from the left edge, and 1.5 inches from the bottom edge.

A suitable command line would be:

gs -sDEVICEWIDTHPOINTS=72 -dDEVICEHEIGHTPOINTS=144 -dFIXEDMEDIA -r300 -sDEVICE=png16m -o out.png -c "<</PageOffset [-180 -108]>> setpagedevice" -f input.pdf

Note that PDF (and PostScript) units are 1/72 inch so 72 = 1 inch, 144 = 2 inches. You need to shift the origin of the page down and left, which is why the values for PageOffset are negative.

If that doesn't work for you I'll need to see your PDF file and you'll need to tell me which version of Ghostscript you are using.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • okay so this worked `-sDEVICEWIDTHPOINTS` should be `-dDEVICEWIDTHPOINTS` but the images in pdf after conversioon to png/jpeg is not good now the complete cmd is like this `"C:\Program Files\gs\gs9.50\bin\gswin64c.exe" ^ -dSAFER -dBATCH -dNOPAUSE ^ -dTextAlphaBits=4 -dGraphicsAlphaBits=4 ^ -dDEVICEWIDTHPOINTS=243 -dDEVICEHEIGHTPOINTS=152 ^ -dFIXEDMEDIA -r600 -dDownScaleFactor=2 -sDEVICE=jpeg -o out3.jpeg ^ -c "<> setpagedevice" ^ -sPDFPassword=ADGE8874 ^ -f "c:\users\ane\desktop\EPAN.pdf"` – jecol Nov 29 '19 at 17:42