phppdffpdffpdi

FPDI slightly crops my imported PDF


I am using FPDI and FPDF and when I import my pdf which has a full-sized, good-looking design, for some reason the generated pdf that I save in a folder is slightly modified in size, a bit smaller / cropped both at the bottom and on the right/left sides. How can I import the PDF as is? With the exact same size as the original? Thanks in advance!

<?php
use setasign\Fpdi\Fpdi;

require_once('FPDF/fpdf.php');
require_once('FPDI/src/autoload.php');

// initiate FPDI
$pdf = new Fpdi();

$pdf->setSourceFile('file.pdf');
$tplidx = $pdf->importPage(1);

$pdf->addPage();
$pdf->useTemplate($tplidx);

$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'This is an example of inputted text.');

$filename="test8.pdf";
$pdf->Output($filename, 'F');

?>

Solution

  • This is a good issue to show that FPDI does not modify a PDF but let you import page into a reusable structure.

    You defined no page size in the constructor nor in the addPage() call, so your resulting page size is A4 (default size).

    If you import a page in e.g. letter format this imported page will not fit onto the A4 page - sure.

    There are some solutions for this:

    A: Get the size of the imported page and call AddPage() with this parameter:

    $pageId = $this->ImportPage($pageNo);
    $s = $this->getTemplatesize($pageId);
    $this->AddPage($s['orientation'], $s);
    $this->useTemplate($pageId); 
    

    B: Set the $adjustPageSize parameter of the useImportedPage() / useTemplate() method to true:

    $this->AddPage();
    $pageId = $this->ImportPage($pageNo);
    $this->useTemplate($pageId, ['adjustPageSize' => true]); 
    

    I would suggest solution A because it's straight forward.