首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用来自两个PDF不同图像通过FPDI/TCPDF创建一个PDF

使用来自两个PDF不同图像通过FPDI/TCPDF创建一个PDF
EN

Stack Overflow用户
提问于 2020-07-06 18:50:19
回答 1查看 86关注 0票数 0

我会使用两个不同的PDF的两个不同的页面创建一个PDF的两个页面的组合。为此,我使用了FPDF,并且我已经这样做了:

代码语言:javascript
复制
$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

但当我运行它时,我发现模板不存在错误。如果我使用$page1而不是$page2 it,那么两个页面将合并在一起。第一个100%大小,第二个75%大小。我不知道为什么$page2不能工作。我已经使用dd(转储模具)来查看这两个$pages之间的区别,没有什么特别的。

因此,我使用了另一种方法,将$page2转换为图片并使用AddImage方法:

代码语言:javascript
复制
$imageFromPDF = "Path_of_my_image.jpg";
$finalPdf->Image($imageFromPDF, 35, 35, $dimension["width"]*0.70, $dimension["height"]*0.70, "JPG");


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

它工作得很好,但质量很差。我看过这个subject,但质量还是很差。

在这两种情况下,有人有好的解决方案吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-07 16:44:40

一步一步来就行了。不需要2个FPDI实例。

代码语言:javascript
复制
$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);

2028在我看来很奇怪,但这正是您所使用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62754488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档