问题描述
是否可以使用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.
任何好的库都可以.
推荐答案
如果您希望从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类.如果我没记错的话,基于pdflib的效果很好,可自定义(如果您了解php),甚至需要一些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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!