title: PDFBox printing with PrintPDF command line tool
body: <p>I am using DHL Shipping (XML) API in order to request DHL shipments and automatically print the responded shipping label. </p>
  
  <p>This is how the system works: </p>
  
  <p>The DHL response XML contains a base64-encoded pdf that contains: </p>
  
  <p>Page 1. The Shipping Label (Prints on a sticker which is put on the package)</p>
  
  <p>Page 2. The Archive Document (Needs to be printed separately and given to the shipping courier) </p>
  
  <p>In order to separate the two pdfs, I first decode the base64, then execute a PDFSplit using PDFBox command-line tools, and finally print each respective document at their respective printer: </p>
  
  <pre><code>$filename = "dhlexpress_labels/".date("Ymd_his")."_{$_REQUEST['id']}.pdf";
  //Get the pdf response from DHL
  $data = file_get_contents("path/dhl_request_shipment.php?".http_build_query($_REQUEST));
  //Put decoded data in the file
  file_put_contents($filename,base64_decode($data));
  //Split the file into two
  $exec = 'java pdfbox-app-1.8.3.jar PDFSplit -split 1 ' . $filename;
  exec($exec);
  //Print each file
  $exec = 'java pdfbox-app-1.8.3.jar PrintPDF -silentPrint -printerName DHLPrinter ' . str_replace('.pdf',-0.'.pdf',$filename);
  exec($exec);
  $exec = 'java pdfbox-app-1.8.3.jar PrintPDF -silentPrint -printerName PaperPrinter ' . str_replace('.pdf',-1.'.pdf',$filename);
  exec($exec);
  </code></pre>
  
  <p>The problem is this: The response from DHL contains a pdf that has the dimensions of an 8"x4" sticker, which prints perfectly on the <code>DHLPrinter</code>, but on the <code>PaperPrinter</code> it doesn't print the left and top 5mm of the label. 
  See attached picture:</p>
  
  <p><img src="https://i.stack.imgur.com/TeW85.jpg" alt="Margins not printed"></p>
  
  <p>I think this happens because the margins are overridden by the pdf somehow, leading to the pdf moving outside the printable area. </p>
  
  <p>Does anyone know of a way to fix this? I want the printing to be completely automated and will stop at no lengths to do so. I'm just wondering if anyone has any creative ideas about how to edit the pdf margins. </p>