PHP 创建 PDF 发票
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5914703/
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 create PDF invoice
提问by Martin
Hi does anyone know how I can create a nicely formatted PDF invoice through PHP?
嗨,有人知道如何通过 PHP 创建格式良好的 PDF 发票吗?
Ideally I'm looking for something with a header and then an itemised listing of the products with some sort of table around. After a quick Google I would be comfortable with generating a PDF but to try and style it nicely would be another thing altogether.
理想情况下,我正在寻找带有标题的东西,然后是带有某种表格的产品的逐项列表。在快速谷歌之后,我对生成 PDF 感到很舒服,但要尝试很好地设计它的样式则完全是另一回事。
Thanks
谢谢
采纳答案by Femi
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:
为此,我使用 TCPDF(请参阅http://www.tcpdf.org/):它功能强大且设置起来不会太痛苦。我会说,根据您的数据源,您可能会遇到一些问题。就我而言,我的数据来自会计系统的 SOAP 接口,并为我的应用程序使用 CodeIgniter,我可以这样做:
$address = $extraclient->get_company_address();
// generate the PDF invoice
$this->load->library('pdfinvoice');
// set document information
$this->pdfinvoice->SetSubject("Invoice " . $data_invoice['code_invoice']);
// add a page
$this->pdfinvoice->AddPage();
$this->pdfinvoice->StartPageOffset();
// write the client's details out
$width = $this->pdfinvoice->GetPageWidth()/2;
$margins = $this->pdfinvoice->getMargins();
$this->pdfinvoice->SetFont('times', 'b', $this->pdfinvoice->bigFont );
$this->_row($width, array("From:", "To:"));
$this->pdfinvoice->SetFont('times', 'i', $this->pdfinvoice->smallFont );
$this->_row($width, array("MY NAME", $customer['name_contact']));
$this->_row($width, array($address['phone'], $customer['name_customer']));
$this->_row($width, array($address['street'], $customer['address1_street']));
$this->_row($width, array($address['city']. ", ".$address['state']." ".$address['zipcode'],
$customer['address1_city']. ", ".$customer['address1_state']." ".$customer['address1_zip
The full code is quite frankly too long to insert here, but you should get the idea, and you get fairly precise layout control.
坦率地说,完整的代码太长而无法在此处插入,但您应该明白这一点,并且您将获得相当精确的布局控制。
回答by Timothy Strimple
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?
我建议创建一个 html/css 表示你想要的 PDF 并使用它来生成 PDF。有几十个应用程序可以处理转换,这里有一个很好的线程,有很多答案:Convert HTML + CSS to PDF with PHP?
回答by Shoaib Shakeel
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.
你可能想看看 html2Pdf。它是一个基于 FPDF 的 php 类,它允许您从 HTML 创建 PDF 文件。因此,只需使用 html 格式化您的文本,然后创建其 pdf。它非常灵活,并提供了很大的控制权。
回答by quickshiftin
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!
php-pdf-invoice
从 Composer检出库。该库已经设计用于生成发票,因此可以轻松快速地集成!
composer require quickshiftin/php-pdf-invoice
composer require quickshiftin/php-pdf-invoice
You can generate PDF files that look like this
您可以生成如下所示的 PDF 文件
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.
README 中有一个使用示例。本质上,您实现了两个接口,一个用于订单,另一个用于订单项。您也可以根据自己的喜好配置字体和颜色。
回答by Ekin Koc
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.
尽管您可以通过编程方式生成 pdf 文档,但我们发现最好只生成 PDF 文档的 HTML 页面并使用某种工具/服务将 HTML 转换为 PDF。
If you are looking for an app that you can host you can check these:
如果您正在寻找可以托管的应用程序,您可以检查这些:
- wkhtmltopdf- uses webkit to render html to pdf. works ok with some caveats.
- PhantomJS- DISCONTINUED, I would not suggest using it.
- Headless Chrome- Best of all but by itself it's not a solution. You need a client module according to your programming language environment.
- wkhtmltopdf- 使用 webkit 将 html 呈现为 pdf。工作正常,但有一些注意事项。
- PhantomJS- DISCONTINUED,我不建议使用它。
- Headless Chrome- 最重要的是,它本身并不是一个解决方案。根据您的编程语言环境,您需要一个客户端模块。
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:
如果您不想自己处理该实用程序,有很多免费/付费在线服务,其中一些列在下面:
- PDF Layer(tested - good)
- Restpack(tested - good)
- DocRaptor
- HTMLPDFAPI
- HTML to PDF Rocket
- PDF 层(已测试 - 良好)
- Restpack(测试 - 良好)
- 猛禽医生
- HTMLPDFAPI
- HTML 到 PDF 火箭
回答by meliniak
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).
PDF 生成往往很棘手。只要发票相对简单,我建议创建一个模板图像文件并用于imagettftext
在其上打印字符串(或任何其他库)。
For image file to PDF conversion imagemagicktool suite can be used (as long as it is available on the server):
对于图像文件到 PDF 的转换,可以使用imagemagick工具套件(只要它在服务器上可用):
exec('convert -units \'PixelsPerInch\' -density 150 '.$filename.'.png '.$filename.'.pdf');
For me the whole approach worked smoothly.
对我来说,整个方法都很顺利。