使用 PHP 将 HTML + CSS 转换为 PDF? [打烊]

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?

转载于:https://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php

csdnceshi62
csdnceshi62 Dompdf is another option. github.com/dompdf/dompdf
大约 2 年之前 回复
weixin_41568174
from.. This question brings back my 2010 memories with PHP and DOMPDF. :)
2 年多之前 回复
csdnceshi55
~Onlooker This question is off-topic at SO, but on-topic in softwarerecs.SE. See How can I convert HTML with CSS to PDF?.
大约 3 年之前 回复
csdnceshi68
local-host You'r right, Chrome headless should render well, but, would run on node, not php.
大约 3 年之前 回复
weixin_41568183
零零乙 I believe soon everyone finds Chrome/Chromium headless chrome --headless --print-to-pdf="path/to/pdf" https://your_url as richest, fastest and easiest html to pdf generation tool crbug.com/603559 as it supports most of html features developers rely on web development and doesn't suck on complex scripts like most of the other libraries and tools do.
3 年多之前 回复
csdnceshi76
斗士狗 check this php5 library github.com/KnpLabs/snappy easy to use and it uses "wkhtmltopdf"
3 年多之前 回复
weixin_41568110
七度&光 How about using Google Chrome, there you get the option to save as pdf from "Print" option.
大约 4 年之前 回复
weixin_41568208
北城已荒凉 you can convert your html w/ styles to a canvas using html2canvas, then convert the canvas to an image using canvasObj.toDataURL("image/jpeg"), append the image to the DOM and pass that to your PHP generator
4 年多之前 回复
csdnceshi52
妄徒之命 Here is a tutorial on DOMPDF. advancetechtutorial.blogspot.com/2015/09/…
4 年多之前 回复
csdnceshi50
三生石@ Sadly, dompdf still has huge issues with table rendering. It adds blank pages in the document
接近 5 年之前 回复
csdnceshi60
℡Wang Yan See also stackoverflow.com/q/10641667/287948
大约 5 年之前 回复
weixin_41568184
叼花硬汉 There are two versions of HTML2PDF. The really old one (2009) linked to here and the newer French one which I talk about in my answer.
5 年多之前 回复
csdnceshi79
python小菜 Other suggestions in this SO post
5 年多之前 回复
csdnceshi75
衫裤跑路 2015 Update: After researching a lot of options we decided to go with wkhtmltopdf its a nice utility we struggle with CSS because we have external CSS file but then we applied the trick of putting a dummy link of CSS on our HTML element which we want to convert and setting the right path it works like MAGIC !!!
5 年多之前 回复
csdnceshi57
perhaps? I gues that the right thing to do is to make browsers produce the pdf becuase they are the only ones doing proper rendering. see this question stackoverflow.com/q/25574082/39998
大约 6 年之前 回复
weixin_41568196
撒拉嘿哟木头 dompdf now supports CSS 2.1 and can deal with @import, @media and @screen rules, and will load external stylesheets. It also comes bundled with everything required for it to work, although there are things you can install to get better performance than the default libs. code.google.com/p/dompdf
接近 8 年之前 回复
csdnceshi63
elliott.david since it's a 2008 question, dompdf is much more mature now. ;-)
大约 9 年之前 回复
csdnceshi53
Lotus@ Html2Pdf actually uses an embedded instance of IE to render the page, then converts that to PDF - probably through IE's print mechanism.
11 年多之前 回复

28个回答

Important: Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2018. Online alternatives like PDFShift are better today at this than they were back then.


Have a look at PrinceXML.

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)

Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?

PrinceXML Samples

weixin_41568110
七度&光 Throwing api2pdf.com into the fold. It's the cheapest api out there and is a wrapper for wkhtmltopdf and headless chrome.
大约 2 年之前 回复
weixin_41568131
10.24 PDFShift only works with the platform right? You get the API Key and just send it over to them? I can't use that because the free plan is limited and I need to generate PDF's for a project of a client, I can't have such constraints and I can't make them pay for it either. Besides that it does look like a very robust solution, just not suitable for me. :(
2 年多之前 回复
csdnceshi58
Didn"t forge : Definitely pdfshift. Check them out!
2 年多之前 回复
weixin_41568131
10.24 What non-online alternative would you recommend for 2018?
2 年多之前 回复
csdnceshi58
Didn"t forge no. because this question is about converting html to pdf. Not generating pdf's from scratch.
2 年多之前 回复
weixin_41568183
零零乙 really? No one mentioned FPDF here?
2 年多之前 回复
csdnceshi52
妄徒之命 MPdf is free, works like a charme, see answer below...
5 年多之前 回复
csdnceshi71
Memor.の One huge benefit is that, the Prince layout engine supports all the page margins: link. But the price is really something.
接近 6 年之前 回复
csdnceshi62
csdnceshi62 If you have commandline access and DON'T want to pay $3500, PhantomJS with this script: github.com/ariya/phantomjs/blob/master/examples/rasterize.js might be a solution - it's the free and easy way!
接近 6 年之前 回复
weixin_41568196
撒拉嘿哟木头 I gues that the right thing to do is to make browsers produce the pdf becuase they are the only ones doing proper css/js rendering. see this question stackoverflow.com/q/25574082/39998
大约 6 年之前 回复
weixin_41568126
乱世@小熊 there are Saas service of equal or better quality for a fraction of the price - see htm2pdf.co.uk
7 年多之前 回复
csdnceshi75
衫裤跑路 Prince or DocRaptor do a pretty bad job on non-valid inputs. wkhtmltopdf or pdfcrowd have a much better render, for cheaper
接近 8 年之前 回复
csdnceshi76
斗士狗 Way too expensive. WKHTMLTOPDF (see other answer) is free, does the job and uses webkit which is awesome.
8 年多之前 回复
weixin_41568127
?yb? I've used DocRaptor, too. Awesome way to get the benefits of Prince without having to pay for the expensive license. Congrats on a great product, Joel.
9 年多之前 回复
csdnceshi56
lrony* My company wrote a web service built around Prince. Significantly cheaper upfront costs, and usable without needing to install anything: docraptor.com
9 年多之前 回复
csdnceshi59
ℙℕℤℝ PrinceXML is really awesome. Only if it was not that expensive :-(
大约 10 年之前 回复
weixin_41568208
北城已荒凉 I've already used it for a big project. Very great tool and the support exists. Just go for it !
大约 10 年之前 回复
weixin_41568174
from.. Well it seems you can only download the desktop version. I'd reeally like to try the server version. But the desktop version did a superb job (equal to my final html2pdf version but virtually instantaneous). Thanks for the recommendation.
11 年多之前 回复

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

A bug was raised against PHP 5.2.1 for this, which is still open almost two years later.

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

  • pcre.backtrack_limit = 2000000; # probably more than I need but that's OK
  • memory_limit = 1024M; # yes, one gigabyte; and
  • max_execution_time = 600; # yes, 10 minutes.

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

Unfortunately, that's the best I've come up with.

csdnceshi76
斗士狗 Grim typo becomes more grim: Memory usage ratio is on the order of 10,000-to-1 lol
接近 4 年之前 回复
csdnceshi80
胖鸭 For the latest version see github.com/spipu/html2pdf
大约 4 年之前 回复
csdnceshi71
Memor.の can you plz tell me about the stable version of HTML2PDF. The above link example has deprecated
大约 7 年之前 回复

Have a look at wkhtmltopdf . It is open source, based on webkit and free.

We wrote a small tutorial here.

EDIT( 2017 ):

If it was to build something today, I wouldn't go that route anymore.
But would use http://pdfkit.org/ instead.
Probably stripping it of all its nodejs dependencies, to run in the browser.

csdnceshi73
喵-见缝插针 Installation on CentOS 6/7 is easy yum --enablerepo=epel-testing install wkhtmltopdf Xvfb. Please note that wkhtml emulates browser so if the page loads google fonts, then access to the internet must be enabled. Also when using via php-fpm then environment variable with path to executable binary must be set e.g. in /etc/php-fpm.d/www.conf set env[PATH] = "/usr/bin" since fpm child process by default cleans parent's environment.
2 年多之前 回复
csdnceshi72
谁还没个明天 wkhtmltopdf is pretty nice. I just built an HTTP API with PHP that allows to pass an URL of a webpage and converts it from HTML to PDF using wkhtmltopdf: github.com/Dellos7/dhtml2pdf
2 年多之前 回复
csdnceshi57
perhaps? Just opened a Github repo to show how to generate an PDF DinA4 invoice using HTML5+CSS+PHP+wkhtmltopdf. Easily adjustable. github.com/rockdaboot/invoice-html5-to-pdf
4 年多之前 回复
csdnceshi51
旧行李 Very nice indeed. I'm just wondering, is it possible not to have selectable text in the output pdf? (I'm on a Win7 Home.) Recognisable text makes the pdf fragile when it comes to printing.
大约 6 年之前 回复
weixin_41568208
北城已荒凉 The commercial services were all too expensive for us so we implemented WKHTMLTOPDF as a free cloud service html2pdfrocket.com for anyone to use, and then used it ourselves for our clients. We did it that way so our clients didn't have to installed exe's on their servers etc and works cross platform. I definitely rate WKHTMLTOPDF if you are building your own service.
6 年多之前 回复
weixin_41568134
MAO-EYE I had the same issue with Debian and Fedora, and years later, it seems to have been pinpointed and solved: github.com/wkhtmltopdf/wkhtmltopdf/issues/45 .
6 年多之前 回复
weixin_41568110
七度&光 where it stores the converted pdf file in windows env for ex. WAMP? When I run command via command tool, it shows progressing and completing however I could not find the output file in my computer? lol.. pls help
接近 7 年之前 回复
weixin_41568196
撒拉嘿哟木头 This abstraction is pretty nice though mikehaertl.github.io/phpwkhtmltopdf
接近 7 年之前 回复
csdnceshi61
derek5. WKHTMLTOPDF is very good. Before I use it, I did my work converting many html to pdf in one day. But with WKHTMLTPDF, now I do my work converting them in just 15 minutes.
7 年多之前 回复
csdnceshi71
Memor.の I recently installed wkhtmltopdf on a Virtualbox CentOS 6 and it worked fine. You don't need root access necessarily, there are static binaries you can wget and use (tested on afromentioned CentOS). I hear lacking X11 might be an issue, but wasn't for me. It is however true that gifs don't work at all. I convert them to PNGs on the fly and generally try to prefer other formats.
大约 8 年之前 回复
csdnceshi62
csdnceshi62 WARNING! If you use wkhtmltopdf (at least on my system, XAMPP on Windows 7 64-bit), in all cases I tried, .gif images fail to appear in the PDF file. I tried a number of workarounds suggested in various places, such as including "width" and "height", and writing the URI's according to different conventions. Nothing I tried ever caused the .gif's to appear (in particular, not even the "width" and "height" suggestion, which I tried both using inline styles and using the archaic, raw "width" and "height" HTML attributes). However, swapping the images to .jpg worked on the first try.
接近 9 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 it's a pity that it's a binary code, you need server root access in order to install the binary. On shared/reseller hosting plans you can forget to use this WKhtmlToPDF.
接近 9 年之前 回复
csdnceshi54
hurriedly% We have had huge problems trying to get this to render fonts properly CentOS servers. After literally weeks of messing around, it seems the only option is not to use CentOS.
大约 9 年之前 回复
weixin_41568174
from.. This is great. To bad it requires the X11 client libs to be installed on the server.
9 年多之前 回复
csdnceshi52
妄徒之命 This one operates on the best premise IMO. Boostrap conversion off an existing renderer instead of writing one from scratch - not a trivial task. Furthermore, Webkit is written in C++ and therefore much faster and much less of a resource hog than PHP based implementation.
10 年多之前 回复

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.

csdnceshi73
喵-见缝插针 I don't recommend 5.7 for the CSS part of the question. Don't expect much styling of your HTML at all.
2 年多之前 回复
weixin_41568126
乱世@小熊 The licensing has also improved for the better. Mpdf is the way to go to create pdf from html and css.
接近 3 年之前 回复
csdnceshi63
elliott.david Claims that it is a lot slower than html2fpdf. But for basic content, I thought it was plenty fast (invoices, reports, etc). The huge plus for mPDF is there are hardly any PHP extension requirements (works out of the box on shared hosting)
4 年多之前 回复
weixin_41568183
零零乙 Well MPDF is working well, true! But the code and documentation are inconsistent - it´s really hard to accomplish a more complex task e.g. creating a letter regarding the din-norm.
5 年多之前 回复
csdnceshi72
谁还没个明天 Version 6.0 works just as expected: include it (or in a framework, load it) instantiate, fill with your stuff, output it a way or another... That's it!
5 年多之前 回复
csdnceshi70
笑故挽风 mPDF giving me some serious hassles when POSTing html tags with image background or tags from database: mPDF error: IMAGE Error (http://www.example.com/folder/image.jpg) Error parsing image file - image type not recognised, and not supported by GD imagecreate
大约 6 年之前 回复
weixin_41568110
七度&光 I tried a bunch of the suggested ones here. So far, this one was uploaded and worked out of the box without any hassle and the docs are truly amazing compared to the rest. Instructions for use are clearly written.
大约 7 年之前 回复
csdnceshi78
程序go It is true, mpdf really works and it is fast, it creates the pdf file on the fly.
7 年多之前 回复

1) use MPDF !

a) extract in yourfolder

b) create file.php in yourfolder and insert such code:

<?php
include('../mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p style="color:red;">Hallo World<br/>Fisrt sentencee</p>');
$mpdf->Output();   exit;
 ?>

c) open file.php from your browser




2) Use pdfToHtml !

1) extract pdftohtml.exe to your root folder:

2) inside that folder, in anyfile.php file, put this code (assuming, there is a source example.pdf too):

<?php
$source="example.pdf";
$output_fold="FinalFolder";

    if (!file_exists($output_fold)) { mkdir($output_fold, 0777, true);}
$result= passthru("pdftohtml $source $output_fold/new_filename",$log);
//var_dump($result); var_dump($log);
?>

3) enter FinalFolder, and there will be the converted files (as many pages, as the source PDF had..)

csdnceshi63
elliott.david GrabzIt is not free
大约 3 年之前 回复
csdnceshi64
游.程 I would try GrabzIt their API supports CSS, JavaScript and most other resource types. It just needs to be referenced with absolute URL's, or made inline and included in the HTML that is sent to the service. Try it here: grabz.it/html-to-pdf-image-api.aspx
接近 4 年之前 回复

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.

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

I've tried a lot of different libraries for PHP. All the listed I've tried. In my opinion TCPDF library is the best compromise performance/usability. It's very simply to install and use, also good performance in small medium application. If you need high performance and very big PDF document, use Zend_PDF module, but get ready to coding hard!

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

csdnceshi62
csdnceshi62 but css style not working
接近 5 年之前 回复
csdnceshi51
旧行李 it's support is for rendering html is rather limited, you might want to read this: tcpdf.org/doc/classTCPDF.html#ac3fdf25fcd36f1dce04f92187c621407
大约 9 年之前 回复

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

weixin_41568174
from.. This issue Vilhelm mentioned has been fixed.
8 年多之前 回复
csdnceshi69
YaoRaoLov Unfortunately impossible to use if you want to generate large PDF-files with a lot of images. I think there is a 60 second timelimit on requests and if Docraptor needs to download a lot of files this will be exceeded, and no file will be made.
9 年多之前 回复
共28条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐