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

Well if you want to find a perfect XHTML+CSS to PDF converter library, forget it. It's far from possible. Because it's just like finding a perfect browser (XHTML+CSS rendering engine). Do we have one? IE or FF?

I have had some success with DOMPDF. The thing is that you have to modify your HTML+CSS code to go with the way the library is meant to work. Other than that, I have pretty good results.

See below:

Original HTML

Converting HTML to PDF

查看更多
步步皆殇っ
3楼-- · 2018-12-31 00:38

I am using fpdf to produce PDF files using PHP. It's working well for me so far to produce simple outputs.

查看更多
谁念西风独自凉
4楼-- · 2018-12-31 00:39

Darryl Hein's mention above of TCPDF is likely a great idea. Nicola Asuni's code is pretty handy and powerful. The only killer is if you ever plan on merging PDF files with your generated PDF it doesn't have those features. You would have to create the PDF and then merge it using something like PDFTK by Sid Steward (www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).

查看更多
泪湿衣
5楼-- · 2018-12-31 00:39

Although there are many solutions offered already, I recommend the following two:

  1. HTM2PDF - offers an API to convert HTML to PDF and also has a PHP SDK, which makes it very easy to implement in PHP; It offers a choice of server locations in Europe, Asia and the USA
  2. PDFmyURL - offers an API that does URL and HTML to PDF as well, with roughly the same functionality as HTM2PDF, but works on a load balanced landscape and has been around a little longer

The thing that's different about these two APIs from all the previously mentioned solutions, is that - besides converting HTML to PDF with CSS and JavaScript - it also offers PDF rights management, watermarking and encryption. Therefore it's an all-in-one solution for those who want to hit the ground running.

Disclaimer: I work for Kaiomi, a company that operates both of these websites.

查看更多
余生请多指教
6楼-- · 2018-12-31 00:40

There's a tutorial on Zend's devzone on generating pdf from php (part 1, part 2) without any external libraries. I never implemented this sort of solution, but since it's all php, you might find it more flexible to implement and debug.

查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 00:41

I suggest DocRaptor (which uses PrinceXML as the "engine")

查看更多
登录 后发表回答