我会使用两个不同PDF中的两个不同页面来创建一个合并了两个页面的PDF。为此,我使用FPDF,我已经这样做了:
$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, PdfReaderPageBoundaries::MEDIA_BOX);
// Import the second page of second PDF
$page2 = $secondPDF->importPage(2, PdfReaderPageBoundaries::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
但当我运行它时,我出现了Template不存在的错误。如果我放$page1而不是$page2,它就工作了,两个页面都是合并的。第一个尺寸为100%,第二个尺寸为75%。我不知道为什么$page2不起作用。我用dd(dump die(来查看两个$page之间的差异,没有什么值得注意的。
所以我使用了另一种方法,将$page2转换为图片,并使用AddImage方法:
$imageFromPDF = "Path_of_my_image.jpg";
$finalPdf->Image($imageFromPDF, 35, 35, $dimension["width"]*0.70, $dimension["height"]*0.70, "JPG");
$pdfFinal->Output("F", "nameOfPdf");
它效果不错,但质量不好。我读过这个主题,但质量仍然很差。
在这两种方式中,有人有一个好的解决方案吗?感谢
循序渐进。不需要2个FPDI实例。
$pdf = new Fpdi();
// set the first document as the source
$pdf->setSourceFile($templatePath);
// then import the first page of it
$page1 = $pdf->importPage(1, PdfReaderPageBoundaries::MEDIA_BOX);
// now set the second document as the source
$pdf->setSourceFile($personalizationPdfPath);
// and import the second page of it:
$page2 = $pdf->importPage(2, PdfReaderPageBoundaries::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);
值20
和28
在我看来很奇怪,但这就是您使用的值。