I’m trying to render Pdfs pages into png files using Ghostscript v9.02. For that purpose I’m using the following command line:
gswin32c.exe -sDEVICE=png16m -o outputFile%d.png mypdf.pdf
This is working fine when the pdf crop box is the same as the media box, but if the crop box is smaller than the media box, only the media box is displayed and the border of the pdf page is lost.
I know usually pdf viewers only display the crop box but I need to be able to see the whole media page in my png file.
Ghostscript documentation says that per default the media box of a document is rendered, but this does not work in my case.
As anyone an idea how I could achieve rendering the whole media box using ghostscript?
Could it be that for png file device, only the crop box is rendered? Am I maybe forgetting a specific command?
For example, this pdf contains some registration marks outside of the crop box, which are not present in the output png file. Some more information about this pdf:
- media box:
- width: 667
- height: 908 pts
- crop box:
- width: 640
- height: 851
OK, now that revers has re-stated his problem into that he is looking for “generic code”, let me try again.
The problem with a “generic code” is that there are many “legal” formal representations of “CropBox” statements which could appear in a PDF. All of the following are possible and correct and set the same values for the page’s CropBox:
/CropBox[10 20 500 700]/CropBox[ 10 20 500 700 ]/CropBox[10 20 500 700 ]/CropBox [10 20 500 700]/CropBox [ 10 20 500 700 ]/CropBox [ 10.00 20.0000 500.0 700 ]/CropBox [ 10 20 500 700 ]The same is true for
ArtBox,TrimBox,BleedBox,CropBoxandMediaBox. Therefor you need to “normalize” the *Box representation inside the PDF source code if you want to edit it.First Step: “Normalize” the PDF source code
Here is how you do that:
qpdffor your OS platform.qpdf --qdf input.pdf output.pdfThe
output.pdfnow will have a kind of normalized structure (similar to the last example given above), and it will be easier to edit, even with a stream editor likesed.Second Step: Remove all superfluous *Box statements
Next, you need to know that the only essential *Box is
MediaBox. This one MUST be present, the others are optional (in a certain prioritized way). If the others are missing, they default to the same values asMediaBox. Therefor, in order to achieve your goal, we can simply delete all code that is related to them. We’ll do it with the help ofsed.That tool is normally installed on all Linux systems — on Windows download and install it from gnuwin32.sf.net. (Don’t forget to install the named “dependencies” should you decide to use the .zip file instead of the Setup .exe).
Now run this command:
sed.exe -i.bak -e "/CropBox/,/]/s#.# #g" output.pdfHere is what this command is supposed to do:
-i.baktells sed to edit the original file inline, but to also create a backup file with a.baksuffix (in case something goes wrong)./CropBox/states the first address line to be processed by sed./]/states the last address line to be processed by sed.stells sed to do substitutions for all lines from first to last addressed line.#.# #gtells sed which kind of substitution to do: replace each arbitrary character (‘.‘) in the address space by blanks (‘‘), globally (‘g‘).We substitute all characters by blanks (instead of by ‘nothing’, i.e. deleting them) because otherwise we’d get complaints about “PDF file corruption”, since the object reference counting and the stream lengths would have changed.
Third step: run your Ghostscript command
You know that already well enough:
All the three steps from above can easily be scripted, which I’ll leave to you for your own pleasure.