PHP 创建 PDF 发票

发布于 2024-11-06 01:08:52 字数 142 浏览 1 评论 0 原文

大家好,有谁知道如何通过 PHP 创建格式良好的 PDF 发票吗?

理想情况下,我正在寻找带有标题的内容,然后是带有某种表格的产品详细列表。经过快速谷歌搜索后,我会很轻松地生成 PDF,但尝试对其进行漂亮的样式设置则完全是另一回事。

谢谢

Hi does anyone know how I can create a nicely formatted PDF invoice through PHP?

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝天白云 2024-11-13 01:08:52

我建议创建一个 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?

柏拉图鍀咏恒 2024-11-13 01:08:52

我使用 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

坦率地说,完整的代码太长,无法插入这里,但您应该明白这个想法,然后您就得到了相当精确的布局控制。

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:

$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.

只是我以为 2024-11-13 01:08:52

您可能想看看 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

陌生 2024-11-13 01:08:52

查看 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

Here's a sample invoice

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.

流云如水 2024-11-13 01:08:52

PDF 生成往往很棘手。只要发票相对简单,我建议创建一个模板图像文件并使用 imagettftext 在其上打印字符串(或任何其他库)。

对于图像文件到 PDF 的转换,可以使用 imagemagick 工具套件(只要它在服务器上可用):

exec('convert -units \'PixelsPerInch\' -density 150 '.$filename.'.png '.$filename.'.pdf');

对我来说,整个方法运行顺利。

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):

exec('convert -units \'PixelsPerInch\' -density 150 '.$filename.'.png '.$filename.'.pdf');

For me the whole approach worked smoothly.

忆梦 2024-11-13 01:08:52

尽管您可以通过编程方式生成 pdf 文档,但我们发现最好只生成 PDF 文档的 HTML 页面并使用某种工具/服务将 HTML 转换为 PDF。

如果您正在寻找可以托管的应用程序,您可以检查以下内容:

  • wkhtmltopdf - 使用 webkit 将 html 渲染为 pdf。工作正常,但有一些注意事项。
  • PhantomJS - 已停用,我不建议使用它。
  • Headless Chrome - 最重要的是,但它本身并不是一个解决方案。您需要根据您的编程语言环境选择客户端模块。

如果您不想自己使用该实用程序,可以使用许多免费/付费在线服务,其中一些服务如下:

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:

  • 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.

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文