Why does fpdi crop a pdf page?

So, I'm using fpdi , version 1.2, to add a small mark to every page in my document. Here is my code:

 public static function markPdf($file, $text){ define('FPDF_FONTPATH','font/'); require_once('fpdi/fpdf.php'); require_once('fpdi/fpdi.php'); try{ $pdf = new FPDI(); $pagecount = $pdf->setSourceFile($file); for($i = 1 ; $i <= $pagecount ; $i++){ $tpl = $pdf->importPage($i); $size = $pdf->getTemplateSize($tpl); // Here you can see that I'm setting orientation for every single page, // depending on the orientation of the original page $orientation = $size['h'] > $size['w'] ? 'P':'L'; $pdf->AddPage($orientation); $pdf->useTemplate($tpl); $pdf->SetXY(5, 5); $pdf->SetTextColor(150); $pdf->SetFont('Arial','',8); $pdf->Cell(0,0,$text,0,1,'R'); } $pdf->Output($file, "F"); }catch(Exception $e){ Logger::log("Exception during marking occurred"); return false; } return true; } 

And everything works fine, except for one small problem: when I have a document with the first page in landscape orientation, all pages in the generated document are cut off from the bottom and to the right (if the first page is in portrait mode, everything goes fine, even if the following pages are in in landscape mode). The question is obvious: what is wrong with this function?

+4
source share
1 answer

OK, finally, I solved the problem. I changed the call
$pdf->useTemplate($tpl);
for the image $pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);
(the most important is the last argument to $adjustPageSize , by default it is false ).
And I also updated all fpdf linked libraries ( fpdf , fpdf_tpl and fpdi ) to the latest versions - this is also important. Hope this will be helpful to someone.

PS: When I clicked the new version of fpdi on the test server, I found that it does not work with the relatively old versions of the PHP interpreter - it works with version 5.3.10, but cannot work with 5.3.2 or later. So make sure you have an updated version of php.

+12
source

Source: https://habr.com/ru/post/1434773/


All Articles