Problem with size of the imported PDF template with FPDI+TCPDF

Viewed 25415

I am stuck in a very complex situation. I am working on a PHP Web apps for Greeting card.

For this, I am using some Linux tools and TCPDF and FPDI. Let me tell you how it all works:

there is 4 page greeting card template PDF file. this is custom size 5x7 inches 300dpi PDF file. I have added custom size in TCPDF as well

case 'STANDARD_CARD'       : {$pf = array(1500.00,2100.00);break;}

what i do is, i use:

pdftk templateX.pdf burst output  page_%2d.pdf 

to separate each page of temple.

now I use :

$pdf = new FPDI($cardDetails['ORIENTATION'],"mm",$cardDetails['SIZE']);      


  //set source file for 
    $pdf->setSourceFile($pdfFile);
    $templateIndex = $pdf->importPage(1);
    $pdf->AddPage($cardDetails['ORIENTATION'],$cardDetails['SIZE']);
    $pdf->useTemplate($templateIndex,0,0);

other things like, writing message printing images. and at the end save the file using:

$pdf->output("file_name.pdf","F");

original PDF file (1st page only): (5x7 inches) Original pdf file Modified PDF and some PDF operations : (29x20 inches) modified PDF

now the output I am getting is not 5x7 pdf it is a 29 x 20 inches file and that destroying my calculation and PDF as well.

Please tell me what I am doing wrong...

3 Answers

I had same problem. My pdf was cropped from right, and from the bottom FDPI was adding space. I found that my pdf had width=215 and height=279, while FPDI was exporting every time 210x297.

useTemplate function can scale your pdf to certain size, but output will still remain 210x297. So I left useTemplate with default values and "adjustPageSize"=true:

$pdf->useTemplate($templateId, 0, 0, 0, 0, true);

What is need to be changed is output dimensions, in order to match original size:

$templateSize = $pdf->getTemplateSize($templateId);
$pdf->AddPage('', [$templateSize['w'], $templateSize['h']]);

If you are going to upload landscape oriented pdfs, you must set orientation:

$templateSize = $pdf->getTemplateSize($templateId);
$orientation = $templateSize['w'] > $templateSize['h'] ? 'L' : 'P';
$pdf->AddPage($orientation, [$templateSize['w'], $templateSize['h']]);
Related