在 PHP 中编写/绘制 PDF 模板文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4299315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing/Drawing over a PDF template document in PHP
提问by filip-fku
I'd like to be able to write/overlay text over an existing pdf document using PHP. What I am hoping to do is have a pdf document that can act as a template, and fill in the gaps by opening the template doc, overlaying the relevant text, and serving the result as a new document. The template document is a single page so page merging/manipulation is not necessary.
我希望能够使用 PHP 在现有的 pdf 文档上编写/覆盖文本。我希望做的是有一个可以充当模板的pdf文档,并通过打开模板文档,覆盖相关文本并将结果作为新文档提供来填补空白。模板文档是单个页面,因此不需要页面合并/操作。
Are there any free libraries that can do this? Anywhere I should look? Most searches I've done seem to deal with merging documents/adding pages, instead of overlaying content over an existing page.
有没有可以做到这一点的免费图书馆?我应该看哪里?我所做的大多数搜索似乎都在处理合并文档/添加页面,而不是在现有页面上覆盖内容。
Thanks.
谢谢。
*EDIT: Here is what I did: 1. Download FPDF 2. Download FPDI + FPDF_TPL from
*编辑:这是我所做的: 1. 下载 FPDF 2. 从以下位置下载 FPDI + FPDF_TPL
http://www.setasign.de/products/pdf-php-solutions/fpdi/downloads/
http://www.setasign.de/products/pdf-php-solutions/fpdi/downloads/
Here is some sample code for any future wanderers (adapted from the samples at www.setasign.de):
以下是一些适用于任何未来流浪者的示例代码(改编自 www.setasign.de 上的示例):
<?php
include('fpdf.php');
include('fpdi.php');
// initiate FPDI
$pdf =& new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('templatedoc.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page as the template
$pdf->useTemplate($tplIdx, 0, 0);
// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(25, 25);
$pdf->Write(0, "This is just a simple text");
$pdf->Output('newpdf.pdf', 'D');
?>
采纳答案by Orbling
Have a look at the FPDI Libraryan add on to FPDFfor template annotation.
It can also bolt-on to TCPDF, another popular PHP PDF library. An existing PDF is used as the base of a page, instead of a blank, after that the procedures are the same as regular PDF creation.
它还可以附加到另一个流行的 PHP PDF 库TCPPDF。现有的 PDF 被用作页面的基础,而不是空白,之后的过程与常规 PDF 创建相同。
回答by Dennis G
You probably want to use PDF Forms for what you want to do. To fill these babies you could use the FDF method described here: Using HTML forms to fill in PDF fields with PHP and FDF.
There is actually another nice SO post about PDF form filling here: Filling PDF Forms with PHP.
您可能想使用 PDF 表单来完成您想做的事情。要填充这些婴儿,您可以使用此处描述的 FDF 方法:使用 HTML 表单使用 PHP 和 FDF 填充 PDF 字段。
实际上还有另一篇关于 PDF 表单填写的好帖子:Filling PDF Forms with PHP。
回答by cchantep
When you want to use PDF lib to write over a document, positionning is time consuming and boring. Dhek is useful for such task: https://github.com/applicius/dhek/releases.
当您想使用 PDF 库来覆盖文档时,定位既费时又乏味。Dhek 对此类任务很有用:https: //github.com/applicius/dhek/releases。
JSON mapping over PDF page/coordinates is defined, so that you can write with PDF API you prefer.
定义了 PDF 页面/坐标上的 JSON 映射,以便您可以使用您喜欢的 PDF API 进行编写。