title: PDF crop even-odd pages with PHP + GhostScript
body: <p>I am working in a routine for crop PDF's and import them into a PDF template. I am using GhostScript, invoked with <code>exec()</code> from a PHP script, and FPDI. All running server-side.</p>
  
  <p>So far, I am able to crop pdf documents with GhostScript using the procedure explained in <a href="https://stackoverflow.com/questions/5797841/cropping-a-pdf-adding-crop-box-using-ghostscript">this post</a> (setting <code>CropBox</code>).</p>
  
  <p>The next step is to crop differently the even and odd pages of a document. So I tried the method explained in <a href="https://superuser.com/questions/904332/add-gutter-binding-margin-to-existing-pdf-file">this other post</a> in SuperUser site, passing custom PostScript code into -c parameter to GhostScript:</p>
  
  <pre><code>-c "&lt;&lt; /CurrPageNum 1 def /Install { /CurrPageNum CurrPageNum 1 add def 
  CurrPageNum 2 mod 1 eq {28 0 translate} {} ifelse } bind  &gt;&gt; setpagedevice"
  </code></pre>
  
  <p>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 <code>sprintf</code> sentence):</p>
  
  <pre><code>-c "&lt;&lt; /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  &gt;&gt; setpagedevice"
  </code></pre>
  
  <p>Here is the full command executed over a 4-page pdf file:</p>
  
  <pre><code>"C:\Program Files (x86)\gs\gs9.07\bin\gswin32c.exe" -sDEVICE=pdfwrite 
  -o C:\inetpub\wwwroot\ledrail\tmp\output.pdf 
  -c "&lt;&lt; /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 &gt;&gt; setpagedevice"
  -f C:\inetpub\wwwroot\ledrail\documentacio\pdf\documentacio_15.pdf
  </code></pre>
  
  <p>Ovbiously, I get an error, because <code>[/CropBox...</code> is not valid PS code.</p>
  
  <pre><code>Error: /typecheck in --.postinstall--
  </code></pre>
  
  <p><strong>EDIT for clarify:</strong></p>
  
  <p>So, my question is: how can I pass <strong><em>the equivalent to</em></strong> two CropBox(es) -for odd and even pages- to the PostScript code shown above? Or, there is another method for achieve this <strong>with GhostScript</strong> from command line?</p>
  
  <p><em>Obviously, I know CropBox is not PostScript valid code, but what are alternatives?</em></p>