PHP 创建 PDF 发票
大家好,有谁知道如何通过 PHP 创建格式良好的 PDF 发票吗?
理想情况下,我正在寻找带有标题的内容,然后是带有某种表格的产品详细列表。经过快速谷歌搜索后,我会很轻松地生成 PDF,但尝试对其进行漂亮的样式设置则完全是另一回事。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议创建一个 html / css 表示您想要 PDF 的内容,并使用它来生成 PDF。有几十个应用程序可以处理转换,这里有一个很好的线程,有很多答案:使用 PHP 将 HTML + CSS 转换为 PDF?
I would recommend creating an html / css representation of what you want a PDF of and using that to generate the PDF. There are dozens of applications to handle the conversion, and there is a good thread with a lot of answers here: Convert HTML + CSS to PDF with PHP?
我使用 TCPDF(参见 http://www.tcpdf.org/)来实现此目的:它非常强大,而且设置起来也不太痛苦。我会说,根据您的数据源,您可能会遇到一些问题。就我而言,我的数据来自会计系统的 SOAP 接口,并在我的应用程序中使用 CodeIgniter,我可以这样做:
坦率地说,完整的代码太长,无法插入这里,但您应该明白这个想法,然后您就得到了相当精确的布局控制。
I use TCPDF (see http://www.tcpdf.org/) for this: its pretty capable and not too painful to get setup. I will say that depending on your data source you may have some issues. In my case my data is sourced from a SOAP interface to my accounting system and use CodeIgniter for my app, and I can do this:
The full code is quite frankly too long to insert here, but you should get the idea, and you get fairly precise layout control.
您可能想看看 html2Pdf。它是一个基于 FPDF 的 php 类,它允许您从 HTML 创建 PDF 文件。因此,只需使用 html 格式化文本,然后创建 pdf 即可。它非常灵活并且具有很强的控制能力。
SourceForge 链接
You may wanna look at html2Pdf. It is a php class based on FPDF and it allows you to create PDF file from HTML. So just format your text using html and then create its pdf. Its very flexible and give greate control.
SourceForge Link
查看
php-pdf-invoice
库作曲家。该库已经设计用于生成发票,因此可以轻松快速地集成!composer require fastshiftin/php-pdf-invoice
您可以生成如下所示的 PDF 文件
自述文件中有一个使用示例。本质上,您实现了两个接口,一个用于订单,另一个用于订单项目。您也可以根据自己的喜好配置字体和颜色。
Checkout the
php-pdf-invoice
library from composer. The library is already designed for generating an invoice so it's easy and quick to integrate!composer require quickshiftin/php-pdf-invoice
You can generate PDF files that look like this
There is a usage example on the README. Essentially you implement two interfaces, one for orders, another for order items. You can configure fonts and colors to your liking as well.
PDF 生成往往很棘手。只要发票相对简单,我建议创建一个模板图像文件并使用 imagettftext 在其上打印字符串(或任何其他库)。
对于图像文件到 PDF 的转换,可以使用 imagemagick 工具套件(只要它在服务器上可用):
对我来说,整个方法运行顺利。
PDF generation tends to be tricky. As long as the invoice is relatively simple, I would recommend creating a template image file and use
imagettftext
to print strings over it (or any other library).For image file to PDF conversion imagemagick tool suite can be used (as long as it is available on the server):
For me the whole approach worked smoothly.
尽管您可以通过编程方式生成 pdf 文档,但我们发现最好只生成 PDF 文档的 HTML 页面并使用某种工具/服务将 HTML 转换为 PDF。
如果您正在寻找可以托管的应用程序,您可以检查以下内容:
如果您不想自己使用该实用程序,可以使用许多免费/付费在线服务,其中一些服务如下:
Even though you can generate pdf documents programmatically, we have found out that it's best if you just generate an HTML page of the PDF document and use some kind of a tool / service to convert the HTML to PDF.
If you are looking for an app that you can host you can check these:
In case you do not wish to handle the utility yourself, there are a lot of free / paid online services, some of them listed below: