使用 PHP 将 HTML 转换为 PDF? [英] Converting HTML to PDF using PHP?

查看:47
本文介绍了使用 PHP 将 HTML 转换为 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

是否可以使用 PHP 将 HTML 页面转换为 PDF,如果可以,如何实现?

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

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

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

http://example.com/invoices/3333

HTML 输出必须转换为 PDF.

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

任何能够做到这一点的优秀库都可以.

Any good libraries that do this will be fine.

推荐答案

如果您希望创建 php 的 pdf,pdflib 将帮助您(正如其他人建议的那样).

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

否则,如果您想通过 PHP 将 HTML 页面转换到 PDF,您会发现这里有点麻烦.. 3 年来我一直尽力做到最好.

Else, if you want to convert an 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.

所以,我知道的选项是:

So, the options I know are:

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

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.

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

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.

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

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:

wkhtmltopdf:基于 webkit(safari 的包装器),非常快速和强大.. 看起来像这是将 html 页面即时转换为 pdf 的最佳方法 (atm);使用 CSS2 处理 3 页 xHTML 文档只需 2 秒.是最近的项目,反正google.code页经常更新.

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.

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

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.

这篇关于使用 PHP 将 HTML 转换为 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆