Search code examples
phppdffpdffpdi

PHP - FPDI is failing to open existing PDF


FPDI is failing to load existing PDF. How to fix it?

enter image description here

require_once(APPLICATION_PATH . '/models/fpdf/fpdf.php');
require_once(APPLICATION_PATH . '/models/fpdi/fpdi.php');
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile( APPLICATION_PATH . '/models/fpdi/en.pdf');
$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);
$pdf->Output(); 

Solution

  •  <?php
    require_once('fpdf.php');
    require_once('fpdi.php');
    

    // initiate FPDI

    $pdf = new FPDI();
    

    // 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 100 mm

    $pdf->useTemplate($tplIdx, 10, 10, 100);
    

    // now write some text above the imported page

    $pdf->SetFont('Helvetica');
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetXY(30, 30);
    $pdf->Write(0, 'This is just a simple text');
    
    $pdf->Output();