Convert HTML + CSS to PDF with PHP? [closed]

2018-12-30 23:53发布

I have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML.

I'm now after a way of converting it to PDF. I have tried:

  • DOMPDF: it had huge problems with tables. I factored out my large nested tables and it helped (before it was just consuming up to 128M of memory then dying--thats my limit on memory in php.ini) but it makes a complete mess of tables and doesn't seem to get images. The tables were just basic stuff with some border styles to add some lines at various points;
  • HTML2PDF and HTML2PS: I actually had better luck with this. It rendered some of the images (all the images are Google Chart URLs) and the table formatting was much better but it seemed to have some complexity problem I haven't figured out yet and kept dying with unknown node_type() errors. Not sure where to go from here; and
  • Htmldoc: this seems to work fine on basic HTML but has almost no support for CSS whatsoever so you have to do everything in HTML (I didn't realize it was still 2001 in Htmldoc-land...) so it's useless to me.

I tried a Windows app called Html2Pdf Pilot that actually did a pretty decent job but I need something that at a minimum runs on Linux and ideally runs on-demand via PHP on the Webserver.

What am I missing, or how can I resolve this issue?

30条回答
泪湿衣
2楼-- · 2018-12-31 00:19

Checkout TCPDF. It has some HTML to PDF functionality that might be enough for what you need. It's also free!

查看更多
心情的温度
3楼-- · 2018-12-31 00:20

Why don’t you try mPDF version 2.0? I used it for creating PDF a document. It works fine.

Meanwhile mPDF is at version 5.7 and it is actively maintained, in contrast to HTML2PS/HTML2PDF

But keep in mind, that the documentation can really be hard to handle. For example, take a look at this page: https://mpdf.github.io/.

Very basic tasks around html to pdf, can be done with this library, but more complex tasks will take some time reading and "understanding" the documentation.

查看更多
泪湿衣
4楼-- · 2018-12-31 00:20

Does the HTML to PDF conversion really need to occur server-side using PHP?

I just came across jsPDF, a client-side solution using HTML5/JavaScript. The MIT-licensed code is on GitHub, too.

查看更多
低头抚发
5楼-- · 2018-12-31 00:23

Web API

If there is people who always search for this kind of stuff, there is a free website which allow you to convert html code & pages to pdf. There is also a (very small) api which allow you to get pdf file from url.

Check it here

查看更多
宁负流年不负卿
6楼-- · 2018-12-31 00:25

I developed a public API to build PDF files from web pages. It has a nice PHP client class that makes it super easy to use. It uses wkhtmltopdf to render the PDF in the cloud.

No need for anything special in the HTML. No need for absolute URLS in images/css/js links. Works on localhost (dev machine) too.

Currently the service has endpoints in 4 Azure regions: US East, US West, EU North, Southeast Asia.

It's fast since it uses a proprietary protocol to send the web page contents to the API for conversion to PDF.

It's reliable because all endpoints are load balanced.

Free account available for testing or low usage. Details on the web site:

https://rotativahq.com

查看更多
春风洒进眼中
7楼-- · 2018-12-31 00:25

not PHP, but a Java library, which does the thing:

Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF

It is usable from PHP via system() or a similar call. Although it requires XML well-formedness of the input.

查看更多
登录 后发表回答