0

I would use two differents pages from two differents PDF for create one PDF with both pages combines. For that I use FPDF and I have done this :

$finalPDF  = new Fpdi();
$secondPDF = new Fpdi();

$personalizationPdfPath   = "Path_of_my_first_PDF";
$templatePath             = "Path_of_my_second_PDF";

$finalPDF->setSourceFile($templatePath);
$secondPDF->setSourceFile($personalizationPdfPath);

// Import the first page
$page1 = $pdfFinal->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);

// Import the second page of second PDF
$page2 = $secondPDF->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);


// Get the size
$dimension = $finalPDF->getTemplateSize($template_page);

// Add the page
$finalPDF->AddPage($dimension["orientation"], array($dimension["width"], $dimension["height"]));

// Apply the page1 on the finalPDF
$finalPDF->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);

// Apply the page2 on the finalPDF
$finalPDF->useTemplate($page2, 20, 28, $dimension["width"]*0.75, $dimension["height"]*0.75); //error

But when I run it, I have Template does not exist error. If I put $page1 instead of $page2 it work, both pages are combine. The first 100% size and second 75% size. I don't know why the $page2 not working. I have used dd(dump die) to see the difference between both $pages, nothing revelant.

So I use an alternative, transform the $page2 into a picture and use AddImage method :

$imageFromPDF = "Path_of_my_image.jpg";
$finalPdf->Image($imageFromPDF, 35, 35, $dimension["width"]*0.70, $dimension["height"]*0.70, "JPG");


$pdfFinal->Output("F", "nameOfPdf");

It works good, but the quality is bad. I have read this subject but the quality still trash.

On both way, anyone has a good solution ? Thanks

D.C
  • 15
  • 3

1 Answers1

0

Just do it step by step. There's no need for 2 FPDI instances.

$pdf = new Fpdi();

// set the first document as the source
$pdf->setSourceFile($templatePath);
// then import the first page of it
$page1 = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);

// now set the second document as the source
$pdf->setSourceFile($personalizationPdfPath);
// and import the second page of it:
$page2 = $pdf->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);

// to get the size of an imported page, you need to pass the 
// value returned by importPage() and not an undefined variable 
// such as $template_page!!
$dimensions = $pdf->getTemplateSize($page1); // or $page2?

// Add a page
$pdf->AddPage($dimension["orientation"], $dimension);

// ...now use the imported pages as you want...
// Apply the page1 on the finalPDF
$pdf->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);

// Apply the page2 on the finalPDF
$pdf->useTemplate($page2, 20, 28, $dimension["width"] * 0.75, $dimension["height"] * 0.75);

The values 20 and 28 looks odd to me but it's what you'd used.

Jan Slabon
  • 4,736
  • 2
  • 14
  • 29