pdf - DOMPDF - 如何在另一个 PDF 文件之上书写文本 (PHP)

标签 pdf merge pdf-generation overlay dompdf

例如,我想加载一个已经存在的 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>&nbsp;</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/

相关文章:

pdf - 在单个四开 PDF 输出中创建多页面方向

python - Pandas:如何将 MultiIndex DataFrame 与单个索引 DataFrame 连接起来,以及自定义排序

R:使用第二个表合并+更新表的有效方法,其中来自相同列名的值填充 NA

latex - 在 RMarkdown 的 pdf 输出中更改内联引用的颜色

c# - OpenHtmlToPdf 访问被 ASP.NET 拒绝

windows - 按文件名搜索文件夹并打印内容

pdf - 使用 AcroForm 技术提交 PDF 表单时的数据编码

java - Servlet 使用 Apache FOP 抛出 Stackoverflow 错误

audio - 合并 2 视频使用 ffmpeg 但我想要

swift - 使用 PDFOutline 将 TOC 添加到 Swift/Cocoa 中的 PDFDocument