How to convert html into pdf with php?

15,825

Solution 1

DOMpdf is the best free one.

If money isn't an issue, PrinceXML is best.

Solution 2

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.

  1. Convert HTML + CSS to PDF with PHP?
  2. Which one is the best PDF-API for PHP?
  3. Export a html into PDF in PHP?
  4. Writing HTML with PHP variables to PDF file?
  5. Tool for exporting html as pdf
  6. Converting HTML in PHP File to PDF File

Solution 3

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.

Solution 4

You may also try using mPDF it's free and has an extensive documentation.

Example:

<?php

include('mpdf/mpdf.php'); 

$mpdf=new mPDF(); 

$mpdf->WriteHTML('<img src="images/logo.png">'); //Write HTML

$mpdf->Output(); //Show the output

?>
Share:
15,825
user586011
Author by

user586011

Updated on June 20, 2022

Comments

  • user586011
    user586011 almost 2 years

    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?

  • Nate365
    Nate365 about 13 years
    Agreed - PrinceXML is the best. If you can't afford the license, DocRaptor.com has a partnership with Prince. You get the same high quality, but you don't have to download anything and the price is much cheaper.