I am working in a routine for crop PDF's and import them into a PDF template. I am using GhostScript, invoked with exec()
from a PHP script, and FPDI. All running server-side.
So far, I am able to crop pdf documents with GhostScript using the procedure explained in this post (setting CropBox
).
The next step is to crop differently the even and odd pages of a document. So I tried the method explained in this other post in SuperUser site, passing custom PostScript code into -c parameter to GhostScript:
-c "<< /CurrPageNum 1 def /Install { /CurrPageNum CurrPageNum 1 add def
CurrPageNum 2 mod 1 eq {28 0 translate} {} ifelse } bind >> setpagedevice"
This method shift odd pages 28 pt, and do none for even pages. So, I tried to modify this, passing CropBox(es) (the %s placeholders are replaced with appropriate coordinates in a sprintf
sentence):
-c "<< /CurrPageNum 1 def /Install { /CurrPageNum CurrPageNum 1 add def
CurrPageNum 2 mod 1 eq {[/CropBox [%s %s %s %s]} {[/CropBox [%s %s %s %s]}
ifelse } bind >> setpagedevice"
Here is the full command executed over a 4-page pdf file:
"C:\Program Files (x86)\gs\gs9.07\bin\gswin32c.exe" -sDEVICE=pdfwrite
-o C:\inetpub\wwwroot\ledrail\tmp\output.pdf
-c "<< /CurrPageNum 1 def /Install { /CurrPageNum CurrPageNum 1 add def
CurrPageNum 2 mod 1 eq {[/CropBox [119.04 168.336 505.92 715.428]}
{[/CropBox [59.52 84.168 505.92 715.428]} ifelse } bind >> setpagedevice"
-f C:\inetpub\wwwroot\ledrail\documentacio\pdf\documentacio_15.pdf
Ovbiously, I get an error, because [/CropBox...
is not valid PS code.
Error: /typecheck in --.postinstall--
EDIT for clarify:
So, my question is: how can I pass the equivalent to two CropBox(es) -for odd and even pages- to the PostScript code shown above? Or, there is another method for achieve this with GhostScript from command line?
Obviously, I know CropBox is not PostScript valid code, but what are alternatives?