Problem with size of the imported PDF template with FPDI+TCPDF
Asked Answered
U

3

13

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...

Unfortunate answered 13/7, 2011 at 6:9 Comment(2)
What is the contents of $cardDetails?Propellant
$cardDetails contain card size and orientation code $cardDetails['ORIENTATION'] = 'L'; $cardDetails['SIZE'] = 'STANDARD_CARD'; code size of STANDARD_CARD is Defiled in TCPDF class as code case 'STANDARD_CARD' : {$pf = array(360.00,504.00);break;} code well, I have my problem solved (to some extent, not 100%) I wanted to set the size of pdf to size of PDF imported, say imported pdf is 5.27 X 7.1. then set the size accordingly. but, no luck in that, just some workable solution, not 100%, but, seems to be working for me...Unfortunate
S
33

Hi Ravish,

I encountered also this issue. Actually my scenario is this. I have an original file which is a Legal size (8.5mm x 14mmmm). When I an displaying it using the FPDI output as you did, it only display a letter size(8.5mm x 11mm). So the result is: CROPPED PDF file.

I made several googling and found several answers too from different posts. Here is the most relevant solution that I found.

First is this piece of function code below: useTemplate

$this->useTemplate($templateIndex, null, null, 0, 0, true);

Normally, some developers set this as TRUE for the last argument. Yes, it is correct if you dont set the width and lenght. However, I would like to emphasize that the 4th and 5th argument specifies the width and length of an imported PDF. So, if you will adopt or get the actual size of the imported document, set the last argument to FALSE as this will tell that it will take the actual or specific size you set.

Please take this sample codes I did:

$pdf = new FPDI();
$pdf -> setSourceFile('birform2316.pdf');
$tplIdx = $pdf -> importPage(1);
$size = $pdf->getTemplateSize($tplIdx);
$pdf -> AddPage();
$pdf ->useTemplate($tplIdx, null, null, $size['w'], 310, FALSE);
$pdf -> SetFont('Arial');
$pdf -> SetTextColor(0, 0, 0);
$pdf -> SetXY(18, 174);
$pdf -> Write(0, $employer_address);
$pdf -> Output('myOwn.pdf', 'D');

With this code, I have produced a new PDF WITHOUT CROPPING the imported file that I set. Meaning, all details of the template(the original file) has been displayed.

Please take note also that I observed something while setting the size of my PDF:

First, my file has an original width of 215.6mm and its length is 350.9mm. Now when I set the size of my PDF using the functions getTemplateSize and useTemplate such as:

$size = $pdf->getTemplateSize($tplIdx);
$pdf ->useTemplate($tplIdx, null, null, $size['w'], $size['h'],FALSE);

or simply:

$pdf ->useTemplate($tplIdx, null, null, 215.6, 350.9,FALSE);

The result is, my new PDF file is CROPPED at the bottom and I dont know why.

With this observation, I made several tests to find out the reason. And the result that came up is, there is a limit of length in generating a PDF file using FPDI. As you can see in my code above, I did not use the actual length of my file. Instead of using 350.9mm which can be derived from $size[h'], I did not use it as it will give a cropped file. I just passed a numeric value near to it and the actual width to produce the desired result.

$pdf->useTemplate($tplIdx, null, null, $size['w'], 310, false);

By the way, 310 mm (length) is the largest numeric value I have used to produce a new PDF file which is NOT CROPPED.

I hope I have given some inputs to all developers using FPDI which encounters the problem of CROPPED PDF results.

Thanks to all...

Levi Palmer

Snooperscope answered 13/7, 2011 at 6:10 Comment(1)
The reason why the pages are cropped is that no entry in FPDIs/FPDFs PageSizes array is created as long as neither the page's orientation nor its size changes (see source code). Since the $size array contains float values, it works to just manually pass an integer for width or height to useTemplate() , because the difference between the float and the int is recognised as a changed page size and hence an entry is added to PageSizes.Keithakeithley
E
14

Instead of

$pdf->useTemplate($templateIndex,0,0);

use

$this->useTemplate($templateIndex, null, null, 0, 0, true);

The last argument $adjustPageSize is set to 'false' by default.

Exact answered 5/6, 2012 at 17:27 Comment(2)
Thanks for this! I was having the same problem. where you have nulls, I had 0s. replacing them with nulls did the trick!Natividadnativism
newer versions will complain if you use zeros for any of the params, so use all nulls, $this->useTemplate($templateIndex, null, null, null, null, true);Capitally
C
5

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']]);
Crandall answered 17/5, 2018 at 8:18 Comment(2)
This works but I needed to use width and height as index instead of w and h. So if you get an undefined index error you know what to do.Exscind
I am using fpdi 1.6.2. This answer is also useful for the older version of fpdi. Thanks!Chinatown

© 2022 - 2024 — McMap. All rights reserved.