Search code examples
phppdfcrop

PDF Crop PHP library


Is there a PHP library that allow to crop each page of a PDF document? I start from a PDF file for printing with crop marks, and I would cut the margins outside the crop marks. I have read that it is possible to do with Ghostscript but can someone recommend a library in PHP that can do the same job? The final PDF should keep the same format as the original file and not transform every page in images.


Solution

  • PDF-Files can contain several boxes that describe the page. There is the MediaBox which describes the area all contents (including crop marks etc) of the document takes up, the CropBox which shall not be used, the BleedBox which describes the area to bleed out images or other stuff so that no white marks pop up after cutting (it's so to say a safe area), and the TrimBox which describes the final format that the printed and trimmed product should have.

    From what I read, your PDF-Document does not contain these boxes (or more correct, all boxes are as great as the MediaBox). As long as you know exactly how great the distance from each border are, you can use a tool such as pdflib+pdi to create a new document containing the pages from the original document but with the correct boxes. Or you can use tcpdf which might be able to set the trim-box of an existing document.

    When you do not know the exact distances the whole thing gets realy complicated as you have to determine the location of the crop-marks programaticaly from the PDF-file to get the appropriate distances. It might be easier to set the appropriate boxes directly in the PDF-Creation-process.

    Ghostscript is perfect for creating Images from PDF-Files but as long as there are no boxes defined it would also use all of the PDF-File. And to create a PDF-File via Ghostscript would mean to print the PDF-File to PostScript-Format and redistilling this PostscriptCode. Takes its time and - depending on your PDF-Version - might be error-prone.

    I'd have a look at one of these Libraries:

    Hope that helps.