Converting HTML to PDF (not PDF to HTML) using PHP
Solution 1
Regarding wkhtmltopdf:
This thing works blazingly fast and it can also handle all kinds of HTML/CSS you throw at it, so when you need speed, you should seriosly consider it. We switch to it recently in our company and our PDF serving got enourmous speed-boost.
At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.
Solution 2
Try this:
- tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
- fpdf.org
- http://code.google.com/p/wkhtmltopdf/
Solution 3
Have you tried Prince?
Solution 4
Try HTMLDOC commandline tool project https://www.msweet.org/htmldoc/index.html
Solution 5
There are many solution to convert HTML to PDF, I can suggest you the one by https://grabz.it.
The have a flexible PHP API which can be used by cronjobs or directly from PHP web page.
If you want to try it, at first you should get an app key + secret for authorization and the development free SDK
Here is an example of a basic implementation.
//First init
include("GrabzItClient.class.php");
// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" values for your account!
$grabzIt = new GrabzItClient("Application Key", "Application Secret");
// To take a PDF screenshot
$grabzIt->URLToPDF("http://www.google.com");
// To save in case public callback handler is available
$grabzIt->Save("http://www.example.com/handler.php");
// OR To save in case public callback handler is not available,
// it's a synchonous method can be usedthe will force your application to wait
// while the screenshot is created
$filepath = "images/result.jpg";
$grabzIt->SaveTo($filepath);
It's possible to get other kinds of screenshots such as image screenshot and etc.
Related videos on Youtube
Santosh S
Updated on September 01, 2020Comments
-
Santosh S almost 4 years
I am a PHP developer and in one of my projects, I need to convert some HTML documents (about 30 to 50 pages) into PDF documents.
My search has turned up the following possible solutions. Among them are some PHP libraries and some command line applications. Each has its own advantages and disadvantages.
PHP libraries:
- fpdf (need more effort to convert)
- tcpdf (need more effort to convert)
- html2fpdf http://html2fpdf.sourceforge.net
- html2pdf http://html2pdf.fr/
- dompdf http://code.google.com/p/dompdf/ (compared to other, works well)
For each library, I have problems like:
- Takes a long time (more than five minutes to convert 30 HTML pages)
-
Requires too many resources (memory and time)
(I set the following parameters in php.ini:
max_execution_time = 600
memory_limit = 250M
but things still don't work.)
Needs HTML pages to be well-formatted (e.g. no missing close tags)
All of these work when I try to convert simple HTML docs (five or fewer pages with little CSS)
Command line applications
All command line apps work perfectly and very quickly compared to the above libraries, but only when I run them directly on console. When I try to use them in PHP with
exec()
orsystem()
, they give me errors.The following are the command line applications and their errors when I run them in PHP:
-
html2pdf (http://www.tufat.com/s_html2ps_html2pdf.htm)
html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0
No protocol specified -
wkhtmltopdf
Loading page: 10%
Loading page: 33%
Loading page: 100%
Waiting for redirect
Outputting pages
QPainter::begin(): Returned false
QPainter::begin(): Returned false
QPainter::save: Painter not active
QPainter::scale: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setBrush: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active htmltopdf (http://www.ultrashareware.com/html-to-pdf.htm)
So now I am looking for help. Can anyone answer:
Which PHP library would work well in my case?
Why do these errors occur in command line applications?
-
Tuy Sotheara almost 15 yearsThe error "Gtk-WARNING **: cannot open display: :0.0" is because the app uses the windowing system. I would guess that the error occurs because the app tries to open the PDF after its generation?
-
Santosh S almost 15 yearsno , it not open pdf after generation. But it open a small window while using it in console.
-
rjmunro almost 14 yearsBecause there are so many questions similar to this one but not quite the same, I decided to try to collect a complete list of HTML to PDF converters into a community wiki question stackoverflow.com/questions/3178448/…
-
Martin Thoma over 6 yearsOff-topic on SO, but softwarerecs.stackexchange.com/q/45903/1834
-
Santosh S almost 15 yearscan you suggests any on-line service and their URL/link
-
Admin almost 15 yearsAnd this could take a while to create big set of PDF.
-
Santosh S over 14 yearsbut, what about links or anchor tag in html pages?
-
Santosh S over 14 yearsdo you know any command line app for same apart from those mentioned in question?
-
Santosh S over 14 years@FDisk , I already tried that and I also mentioned same in my question
-
Santosh S over 14 yearshmmmmmm? but its expensive, cost USD 495 !
-
Randell over 14 yearsFor PHP, i've only used dompdf, and I only have to print an average of 3 pages per call. The only other PDF generator i've used is JasperReports, but I think it's only for Java. Maybe you could most the entire stack trace of the error you're getting from the command line.
-
FDisk over 14 yearsfreepdfconvert.com has no support for flash files and javascript. And generated pdf is not exactly as the website looks. for example i tried pazintys.com
-
rjmunro almost 14 yearsThat is a terrible solution. It will turn all the text into bitmap graphics. It will use screen css instead of print css. It will only show as much of the page as can fit in a screen capture. There are plenty of ways to do it better. Please don't do this!
-
Julie over 13 yearsDocRaptor.com uses Prince as the engine (so you get the same awesome results), but it's much cheaper (free for small plans).
-
Admin over 13 yearsHmm. Your answer history would suggest you have some sort of interest in this service Julie - how about a little full disclosure eh?
-
andho almost 13 yearsIt fails poorly with Multipage tables
-
andho almost 13 yearsPrince rocks, but too expensive :(
-
andho almost 13 yearsPoor formatting, does not support CSS2.1 yet
-
Carlos2W over 8 yearsNo it doesn't. You just have to handle this kind of problem with css : stackoverflow.com/questions/1763639/…