例如,我想加载一个已经存在的 PDF 文件,然后在该 PDF 文件之上转置文本并将其再次保存为一个新的 PDF 文件。本质上是将两者合并,将一个调换到另一个上。
我不希望作为背景的 PDF 变成图像而失去质量,因此所有内容都保留其原始矢量格式。
我将如何进行合并?
我不想使用软件,也许有一个脚本可以做到这一点?
最佳答案
dompdf 本身无法执行此类操作。您需要独立生成 PDF 文档,然后使用第三方库合并这两个文档。
由于您使用的是 dompdf,我猜您可能正在寻找基于 PHP 的解决方案。看看FPDI .使用 dompdf 生成 PDF 文档后,您可以使用 FPDI 将它们组合起来。
我认为这会起作用,但我还没有测试过:
<?php
// generate pdf documents
require_once('dompdf/autoload.inc.php');
$dompdf = new Dompdf();
$dompdf->loadHtml('<p>Hello</p>');
$dompdf->render();
file_put_contents('doc1.pdf', $dompdf->output());
unset($dompdf);
$dompdf = new Dompdf();
$dompdf->loadHtml('<p> </p><p>Hello</p>');
$dompdf->render();
file_put_contents('doc2.pdf', $dompdf->output());
// combine pdf documents
require_once('fpdf.php');
require_once('fpdi.php');
// initiate FPDI
$pdf = new FPDI('L');
// add a page
$pdf->AddPage();
// set the source file to doc1.pdf and import a page
$pdf->setSourceFile("doc1.pdf");
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 210 mm
$pdf->useTemplate($tplIdx, 10, 10, 210);
// set the source file to doc2.pdf and import a page
$pdf->setSourceFile("doc2.pdf");
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 100,10 with a width of 210 mm
$pdf->useTemplate($tplIdx, 100, 10, 210);
$pdf->Output();
关于pdf - DOMPDF - 如何在另一个 PDF 文件之上书写文本 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36634264/