I have html in a mysql table and want to turn it into downloadable pdf files.
There are tools to convert files into pdf, but I have not found one that works with php.
Can you help? Any suggestions?
I have html in a mysql table and want to turn it into downloadable pdf files.
There are tools to convert files into pdf, but I have not found one that works with php.
Can you help? Any suggestions?
If your meaning is to create a pdf from php, pdflib will help you.
Else, if you want to convert an HTML page in pdf via PHP, the options I know are:
DOMPDF : php class that wrap the html and build the pdf. Works good, customizable (if you know php), based on pdflib, if i remember right it takes even some CSS. Bad news: slow when the html is big or complex.
HTML2PS: same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). For me this is little better than dompdf, but have the same speed problem.. oh, better compatibility with css.
Those two are php classes, but if you can install some software on the server, and access it through passthru() or system(), give a look to these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerful.. seem like is the best one (atm) for convert on the fly html pages to pdf, taking only 2 seconds for a 3 pages xHTML document with CSS2. Is a recent project, anyway, the google.code page is often updated.
htmldoc : this one is a tank, it really never stop/crash.. the project seem death in the 2007, but anyway if you don't need css compatibility this can be nice for you.
tcpdf - this is an enhanced and maintained version of fpdf. Main Features of tcpdf and it is also having less execution time with great output. For a detailed tutorial on using the two most popular pdf generation classes: TCPDF and FPDF. Please follow this link.
See this posts also.
The html2pdf library might help you ; I've heard it's generally doing nice job -- but might require you to adapt your HTML a little bit.
I'm surprised nobody mentioned it, but you can also use online APIs to do the job for you. Depending on your usage, it can be free and you can rely on their up-to-date features (like CSS3 and WebFonts) and you externalize the processing, which reduces the hardware work (or give it more resources to handle other requests).
There are a lot of APIs dedicated to converting HTML to PDF. PDFShift is one of them (I work at), and the syntax is really simple (of course it differs from API).
For instance, converting an URL to PDF is as simple as doing a curl_
POST request:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.pdfshift.io/v2/convert/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode({'source': 'https://pdfshift.io/documentation'}),
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_USERPWD => 'YOUR_API_KEY:'
));
$response = curl_exec($curl);
file_put_content('pdfhsift-documentation.pdf', $response);
Most of the online APIs have the same syntax, more or less, and differs in term of price, performance, processing, etc.