开发者

Rendering the whole media box of a pdf page into a png file using ghostscript

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 anyo开发者_如何学编程ne 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, CropBox and MediaBox. 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:

  1. Download qpdf for your OS platform.
  2. Run this command on your input PDF:
    qpdf --qdf input.pdf output.pdf

The output.pdf now 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 like sed.

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 as MediaBox. 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 of sed.

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:

  1. sed.exe -i.bak -e "/CropBox/,/]/s#.# #g" output.pdf

Here is what this command is supposed to do:

  • -i.bak tells sed to edit the original file inline, but to also create a backup file with a.bak suffix (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.
  • s tells sed to do substitutions for all lines from first to last addressed line.
  • #.# #g tells 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:

gswin32c.exe -sDEVICE=png16m -o outputImage_%03d.png output.pdf

All the three steps from above can easily be scripted, which I'll leave to you for your own pleasure.


First, let's get rid of a misunderstanding. You wrote:

"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."

That's not correct. If the CropBox is smaller than the MediaBox, then only the CropBox should be displayed (not the MediaBox). And that is exactly how it was designed to work. This is the whole idea behind the CropBox concept...


At the moment I cannot think of a solution that works automatically for each PDF and all possibly values that can be there (unless you want to use payware).

To manually process the PDF you linked to:

  1. Open the PDF in a good text editor (one that doesn't mess with existing EOL conventions, and doesn't complain about binary parts in the file).
  2. Search for all spots in the file that contain the /CropBox keyword.
  3. Since you have only one page in the PDF, it should find only one spot.
  4. This could read like /CropBox [12.3456 78.9012 345.67 890.123456].
  5. Now edit this part, carefully avoiding to add to (or lose from) the number of already existing characters:
  6. Set the value to your wanted one: /CropBox [0.00000 0.00000 667.00 908.000000]. (You can use spaces instead of my .0000.. parts, but if I do, the SO editor will eat them and you'll not see what I originally typed...)
  7. Save the file under a new name.
  8. A PDF viewer should now show the full MediaBox (as of your specification).
  9. When you convert the new file with Ghostscript to PNG, the bigger page will be visible.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜