php 带有PDF输出的PHP PDF模板库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4416667/
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
PHP PDF template library with PDF output?
提问by Pekka
Is there any PHP PDF library that can replace placeholder variablesin an existing PDF, ODT or DOCX document, and generate a PDF file as the end result, without screwing up the layout?
是否有任何 PHP PDF 库可以替换现有 PDF、ODT 或 DOCX 文档中的占位符变量,并生成 PDF 文件作为最终结果,而不会弄乱布局?
Requirements:
要求:
Needs no 3rd party web service
Ability to run on shared web hosting would be ideal (no binary installations / packages required)
不需要第 3 方网络服务
能够在共享网络托管上运行将是理想的(不需要二进制安装/包)
Mind you, a library that is able to load an existing PDF file and insert text programmatically at a specific position is not enoughfor my use case.
请注意,一个能够加载现有 PDF 文件并以编程方式在特定位置插入文本的库对于我的用例来说是不够的。
As far as my research shows, there is no library that can do this:
据我的研究表明,没有图书馆可以做到这一点:
TCPDF can only generate documents from scratch
FPDI can read existing PDF templates, but can only add contents programmatically (no template variable replacement)
There are various DOCX/ODT template libraries out there but they don't output PDF
TCPDF 只能从头开始生成文档
FPDI 可以读取现有的 PDF 模板,但只能以编程方式添加内容(无模板变量替换)
有各种 DOCX/ODT 模板库,但它们不输出 PDF
PHPDOCxclaims to be able to do exactly what I need - but they don't offer a trial version and I'm not going to buy a cat in a bag, especially not when there seems to be no other producton the web that does this. I find it hard to believe they can do this without problems - if you have successfully done this using the product, please drop a line here.
PHPDOCx声称能够完全满足我的需要——但他们不提供试用版,我不会买袋装猫,尤其是当网络上似乎没有其他产品时这个。我发现很难相信他们可以毫无问题地做到这一点 - 如果您使用该产品成功做到了这一点,请在此处留言。
Am I overlooking something?
我是否忽略了什么?
Is there a way to do this using PDF forms? I am creating the source documents in OpenOffice 3.
有没有办法使用 PDF 表单来做到这一点?我正在 OpenOffice 3 中创建源文档。
I may be able to use standard Linux commands (pdftk
is available for example, trying that out right now.)
我也许可以使用标准的 Linux 命令(pdftk
例如,现在就可以尝试。)
Update:*Argh!* I was called out of the office and the bounty expired in the meantime. Starting a new bounty: As far as my testing shows, no solution works for me perfectly yet.
Update II:I will be looking the pdftk approach soon, but I am also starting another bounty for one more round of collecting additional input. This question has now seen 1300 rep points in bounties, must be some kind of a record :)
更新:*啊!* 我被叫出办公室,赏金在此期间到期。开始新的赏金:据我的测试显示,目前还没有任何解决方案适合我。
更新二:我很快就会寻找 pdftk 方法,但我也开始了另一轮赏金,以收集更多的额外输入。这个问题现在已经看到了 1300 代表点的赏金,一定是某种记录:)
采纳答案by Orbling
Pekka,
佩卡,
I looked in to this previously, I think you can use pdftk(a command line utility), to fill in a PDF form using FDF/XFDF data files, which you could easily generate from within PHP. That was the best option I've seen so far, though there may well be a native library.
我以前看过这个,我认为您可以使用pdftk(一个命令行实用程序),使用 FDF/XFDF 数据文件填写 PDF 表单,您可以在 PHP 中轻松生成这些文件。这是迄今为止我见过的最好的选择,尽管很可能有一个本地库。
pdftkis quite useful in general, worth having a look at.
pdftk通常非常有用,值得一看。
Update:Have a look here: http://php.net/manual/en/book.fdf.php
更新:看看这里:http: //php.net/manual/en/book.fdf.php
回答by mario
This is not very practical, but for completeness: If you already have an ODT template, then you might very well retain that as template. Modifying the OpenDocument content.xml and replacing placeholders therein is pretty simple. If so, you could use unoconv
or pyodconverter
to transform the ODT into a final PDF.
这不是很实用,但为了完整性:如果您已经有一个 ODT 模板,那么您很可能将其保留为模板。修改 OpenDocument content.xml 并替换其中的占位符非常简单。如果是这样,您可以使用unoconv
或pyodconverter
将 ODT 转换为最终的 PDF。
unoconv -f pdf -o final.pdf template.odt
Very obviously this requires a full OpenOffice setup (UNO and Writer) on the webserver. And obviously not every webhoster would go with that! haha. Even if it's simple on any Debian or Fedora setup. The execution speed would probably not be stellar either. But then it might be the cleanest approach, since OOo governs both formats way better than any PHP class ever could.
很明显,这需要在网络服务器上进行完整的 OpenOffice 设置(UNO 和 Writer)。显然不是每个网络托管商都会这样做!哈哈。即使在任何 Debian 或 Fedora 设置上都很简单。执行速度也可能不会很出色。但它可能是最干净的方法,因为 OOo 比任何 PHP 类都更好地管理这两种格式。
回答by CaseySoftware
Have you considered using something like XSL:Formatting Objects (XSL:FO)? Basically they're XML documents that are processed and turned into PDFs. Doing string - or better, DOM - replacements within that should be pretty simple. It supports embedding images, links, annotations, etc.
您是否考虑过使用 XSL:Formatting Objects (XSL:FO) 之类的东西?基本上,它们是经过处理并转换为 PDF 的 XML 文档。在其中进行字符串 - 或者更好的 DOM - 替换应该非常简单。它支持嵌入图像、链接、注释等。
It's not PHP but there are a number of PHP wrappers for it along with ways of using it via exec, etc. Not an ideal but it takes care of the template portion completely. For some more info: http://techportal.inviqa.com/2009/12/16/transforming-xml-with-php-and-xsl/
它不是 PHP,但有许多 PHP 包装器以及通过 exec 等使用它的方法。不是理想的,但它完全处理了模板部分。有关更多信息:http: //techportal.inviqa.com/2009/12/16/transforming-xml-with-php-and-xsl/
There's an implementation available as an Apache project - http://xmlgraphics.apache.org/fop/
有一个可用作 Apache 项目的实现 - http://xmlgraphics.apache.org/fop/
回答by renevdkooi
fpdf and there is another extention on top of it, which I can't remember, which allows you to import templates
fpdf 上面还有另一个扩展,我不记得了,它允许您导入模板
回答by John Cartwright
Your best bet would be to generate the entire document on the fly, with the template defined programatically using fpdf or something similar. That way, your text will not be cut off by paragraphs or anything like that, and you can easily position images/other elements as required.
最好的办法是即时生成整个文档,使用 fpdf 或类似的东西以编程方式定义模板。这样,您的文本就不会被段落或类似内容截断,您可以根据需要轻松定位图像/其他元素。
回答by cchantep
Late, but you can use OpenSource template designer https://github.com/applicius/dhek/releases, to define pkaceholders/areas over any existing PDF, then load it in PHP (as it's JSON format) and write accordingly on original PDF using fpdf lib, to generate custom PDF with dynamic data written on.
晚了,但您可以使用 OpenSource 模板设计器https://github.com/applicius/dhek/releases在任何现有 PDF 上定义 pkaceholders/areas,然后将其加载到 PHP(因为它是 JSON 格式)并在原始 PDF 上相应地编写使用 fpdf lib,生成带有动态数据的自定义 PDF。
回答by Gajahlemu
Ok I'm trying to help you solve the problem a little.
好的,我正在尝试帮助您解决一些问题。
First the answer for couple of your question.
首先回答你的几个问题。
Q - Am I overlooking something?
问 - 我是否忽略了什么?
A - No. There is a PHP PDF library that can replace placeholder variables in an existing PDF and generate a PDF file as the end result, without screwing up the layout
A - 不。有一个 PHP PDF 库可以替换现有 PDF 中的占位符变量并生成 PDF 文件作为最终结果,而不会搞砸布局
Q - Is there a way to do this using PDF forms?
问 - 有没有办法使用 PDF 表单来做到这一点?
A - Yes. absolutelly the tric to doing this is by using a PDF Forms
A - 是的。绝对做到这一点的技巧是使用 PDF 表单
For both answer you can use Justin Koivisto fill pdf form field php library. For more detail you please go to http://koivi.com/fill-pdf-form-fields/tutorial.php. Take a look there for additional information.
对于这两个答案,您可以使用 Justin Koivisto 填写 pdf 表单字段 php 库。有关更多详细信息,请访问http://koivi.com/fill-pdf-form-fields/tutorial.php。看看那里的其他信息。
Credit to Justin Koivisto for his work
归功于 Justin Koivisto 的工作
P.S
聚苯乙烯
For workaround for displaying a table like output from pdf form please consider to take some reading on Oracle Business Intelligence Publisher User's Guide - Creating a PDF Template
有关显示 pdf 表单输出等表格的解决方法,请考虑阅读Oracle Business Intelligence Publisher 用户指南 - 创建 PDF 模板
回答by Capsule
I'll add this new answer since the FDF PHP extension is now dead.
我将添加这个新答案,因为 FDF PHP 扩展现在已经死了。
I've just followed these instructionsand ended up executing one perl script then the pdftk command
我刚刚按照这些说明操作,最后执行了一个 perl 脚本,然后执行了 pdftk 命令
I'm pretty aware it's far from being a real PHP solution but it's reliable and fairly easy to implement on any *nix platform.
我很清楚它远非真正的 PHP 解决方案,但它在任何 *nix 平台上都可靠且相当容易实现。
The tools described there are also available on Debian, just in case you were wondering.
那里描述的工具也可以在 Debian 上使用,以防您想知道。
回答by ts.
Altough not exactly thing you asked, you may consider to make it at two steps: using some php templating sytem (smarty, dwoo) to generate html page and then using tools like Html2Pdfconvert it to pdf. I am using it, and results are good (no problems with page layout etc)
尽管不完全是您问的问题,但您可以考虑分两步进行:使用一些 php 模板系统(smarty,dwoo)生成 html 页面,然后使用Html2Pdf 等工具将其转换为 pdf。我正在使用它,结果很好(页面布局等没有问题)
Of course it depends of your input documents (can you use html instead of PDF/ ODT as source ) and complexity of the layout of those.
当然,这取决于您的输入文档(您可以使用 html 而不是 PDF/ODT 作为源文件)和这些布局的复杂性。
回答by Nikolai Fischer
It's a litte bit late but have a look at the PDFTemplateLibrary it does exatly what you want. You can create Open Document files (odt) and add placeholders in it. The PDFTemplate library can fill out these placeholders (even with images) and create a PDF file.
有点晚了,但看看PDFTemplate库,它完全符合您的要求。您可以创建 Open Document 文件 (odt) 并在其中添加占位符。PDFTemplate 库可以填写这些占位符(甚至包含图像)并创建 PDF 文件。