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

The HTML2PDF and HTML2PS that was originally mentioned in opening post was talking about a 2009 package with this link

But there is a better HTML2PDF

It is based on TCPDF though it is partly in French.

You can have table headers or footers that repeat on the pages and have page numbers and total pages. See its examples. I have been using it for over three years and recommend it.

查看更多
有味是清欢
3楼-- · 2018-12-31 00:35

If you have access to the command line it's possible to use PhantomJS to create the PDF from an URL (remote or local).

It works really well, and is a free solution.

Take a look at this example script made for this exact problem.

查看更多
笑指拈花
4楼-- · 2018-12-31 00:36

Just to bump the thread, I've tried DOMPDF and it worked perfectly. I've used DIV and other block level elements to position everything, kept it strictly CSS 2.1 and it played veru nicely.

查看更多
余生无你
5楼-- · 2018-12-31 00:36

Fine rendering doesn't mean anything. Does it validate?

All browsers do the most they can to just show something on the screen, no matter how bad the input. And of course they do not do the same thing. If you want the same rendering as FireFox, you could use its rendering engine. There are pdf generators for it. It is an awful lot of work, though.

查看更多
不流泪的眼
6楼-- · 2018-12-31 00:37

Good news! Snappy!!

Snappy is a very easy open source PHP5 library, allowing thumbnail, snapshot or PDF generation from a url or a html page. And... it uses the excellent webkit-based wkhtmltopdf

Enjoy! ^_^

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

It's already been mentioned, but I'd just like to confirm that mpdf is the easiest, most powerful and most free html to pdf converter out there. The sky's really the limit. You can even generate pdfs of dynamic, user generated data.

For instance, a client wanted a CMS system so he could update the track list of the music he played at his club. That was no problem, but he also wanted users to be able download a .pdf of the playlist, and so this downloadable pdf had to be updated by the cms too. Thanks to mpdf, with some simple loops and interspersed variables I could do just that. Something that I thought would take me weeks literally took me minutes.

Great article that helped me get started.

查看更多
登录 后发表回答