使用 PHP 将 HTML 转换为 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733219/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:41:11  来源:igfitidea点击:

Converting HTML to PDF using PHP?

phppdfpdf-generation

提问by Click Upvote

Possible Duplicate:
Convert HTML + CSS to PDF with PHP?

可能的重复:
使用 PHP 将 HTML + CSS 转换为 PDF?

Is it possible to convert a HTML page to PDF using PHP, and if so, how can it be done?

是否可以使用 PHP 将 HTML 页面转换为 PDF,如果可以,该怎么做?

Specifically, the page is an invoice generated dynamically. So I would like it loaded using:

具体来说,页面就是一张动态生成的发票。所以我希望它加载使用:

http://example.com/invoices/3333

And the HTML output would have to be converted to PDF.

并且 HTML 输出必须转换为 PDF。

Any good libraries that do this will be fine.

任何能做到这一点的好库都可以。

回答by Strae

If you wish to createa pdf from php, pdflib will help you (as some others suggested).

如果您想从 php创建pdf,pdflib 会帮助您(正如其他人建议的那样)。

Else, if you want to convertan HTML page to PDF via PHP, you'll find a little trouble outta here.. For 3 years I've been trying to do it as best as I can.

否则,如果您想通过 PHPHTML 页面转换为 PDF,您会发现这里有点麻烦.. 3 年来,我一直在尽我所能地做到这一点。

So, the options I know are:

所以,我知道的选项是:

DOMPDF: php class that wraps the html and builds the pdf. Works good, customizable (if you know php), based on pdflib, if I remember right it takes even some CSS. Bad news: slow when the html is big or complex.

DOMPDF:包装 html 并构建 pdf 的 php 类。工作良好,可定制(如果你知道 php),基于 pdflib,如果我没记错的话,它甚至需要一些 CSS。坏消息:当 html 很大或很复杂时会变慢。

HTML2PS: same as DOMPDF, but this one converts first to a .ps (ghostscript) file, then, to whatever format you need (pdf, jpg, png). For me is little better than dompdf, but has the same speed problem.. but, better compatibility with CSS.

HTML2PS:与 DOMPDF 相同,但此文件首先转换为 .ps(ghostscript)文件,然后转换为您需要的任何格式(pdf、jpg、png)。对我来说比 dompdf 好一点,但有同样的速度问题..但是,与 CSS 的兼容性更好。

Those two are php classes, but if you can install some software on the server, and access it throught passthru() or system(), give a look to these too:

这两个是 php 类,但如果你可以在服务器上安装一些软件,并通过 passthru() 或 system() 访问它,也看看这些:

wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerful.. seems like this is the best one (atm) for converting html pages to pdf on the fly; taking only 2 seconds for a 3 page xHTML document with CSS2. It is a recent project, anyway, the google.code page is often updated.

wkhtmltopdf:基于 webkit(safari 的包装器),非常快速和强大。使用 CSS2 的 3 页 xHTML 文档只需 2 秒。是最近的项目,反正google.code页面经常更新。

htmldoc: This one is a tank, it never really stops/crashes.. the project looks dead since 2007, but anyway if you don't need CSS compatibility this can be nice for you.

htmldoc:这是一个坦克,它从来没有真正停止/崩溃..该项目自 2007 年以来看起来已经死了,但无论如何,如果您不需要 CSS 兼容性,这对您来说会很好。