Search code examples
phppdftcpdffpdfdompdf

Script to take existing PDF file, clone to double 1/2 page format on output PDF


EDITTED: If there's any confusion involved, the image towards the bottom is portrait layout on the left, landscape layout on the right. Meaning, the pdf contents that were on the left hand side are to be cloned side by side to each other, into a landscape format.

The resources I'm using at the moment are FPDF, DOMPDF, and about to incorporate TCPDF into this project, coded in PHP.

I need to know if it's possible to use any of these resources, or any other resource for that matter, to take an existing PDF file and clone it to where it takes up exactly 2 half pages of the output PDF. The most crucial part about this is that there should be no borders/margins.

Does anyone have a script that can do this flawlessly?

Here's an image representation:

Flipped Page


Solution

  • Take a look at at FDPI (which is based on FPDF). The simple demo shows importing a PDF into a portion of a page.

    I haven't used it, but based on that demo you should be able to do what you want using something like the following:

    <?php
    require_once('fpdf.php');
    require_once('fpdi.php');
    
    // initiate FPDI
    $pdf = new FPDI('L');
    // add a page
    $pdf->AddPage();
    // set the source file
    $pdf->setSourceFile("PdfDocument.pdf");
    // import page 1
    $tplIdx = $pdf->importPage(1);
    // use the imported page and place it at point 10,10 with a width of 150 mm
    $pdf->useTemplate($tplIdx, 10, 10, 150);
    // use the imported page and place it at point 400,10 with a width of 150 mm
    $pdf->useTemplate($tplIdx, 400, 10, 150);
    
    $pdf->Output();
    

    (Note: this is completely untested and the fact that the width is in mm while the positioning is in PT makes this a rough estimate at best.)