I have a PHP file that generates a report from the database, and I want this page to be converted into a PDF file, so it can be saved and printed. How do I convert a web page to a PDF? Is there any tool available or a PHP script?
-
1use http://www.fpdf.org/?lang=en – Pandiyan Cool Aug 08 '13 at 06:44
-
1check this [stackoverflow](http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php) answer – f.vincent Aug 08 '13 at 06:45
-
1possible duplicate of [Webpage convert to PDF button](http://stackoverflow.com/questions/8050318/webpage-convert-to-pdf-button) – Pandiyan Cool Aug 08 '13 at 06:46
-
1this link may help you http://www.web-to-pdf.com/ – Aug 08 '13 at 06:56
-
@PandiyanCool, can't make it work on fpdf, I mean I have to follow a format, and it has a lot of tables, 1 header 2 columns (different size). – QKWS Aug 08 '13 at 10:06
-
1use a save as PDF function like the one from HTM2PDF or something else – user1914292 Aug 08 '13 at 19:26
6 Answers
You can use MPDF, a PHP library which generates PDF files from UTF-8 encoded HTML. It's under GNU GPL v2 licence.

- 4,190
- 2
- 30
- 39

- 1,763
- 1
- 10
- 14
-
thanks for this, tried each of the answer given to me, and this is what I like the most, esp. converting HTML to PDF and its extensive documentation and this is I think the one I'll be using. – QKWS Aug 10 '13 at 01:03
-
5Both links are dead. **This is a good example why link only answer are BAD.** – Pedro Lobito May 20 '18 at 20:35
-
-
2
Yes.
Make use of TCPDF Plugin.
Here's an example and try this after configuring the above on your code.
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<h1>Example of HTML text flow</h1>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. <em>Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?</em> <em>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</em><br /><br /><b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i><br /><br /><b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('example_021.pdf', 'I');
?>

- 68,075
- 43
- 96
- 126
-
1
-
@QKWS, You don't need to specify all that. Just try the example above and let know. [I have edited the old code] – Shankar Narayana Damodaran Aug 08 '13 at 10:10
I used pretty often:
Made so far a good job, there are a few bugs but else it does very well.
With TCPDF I had the most problems, since it seems that it doesn't support the convertation from HTML to PDF that well.

- 655
- 4
- 9
If you want generate PDF client-side (with a button on the html report), you could use jsPDF (http://parall.ax/products/jspdf)

- 556
- 1
- 9
- 28
You can use wkhtmltopdf or tcpdf scripts to convert html pages to pdf

- 2,260
- 1
- 25
- 42
DomPDF looks like what you're looking for.
dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
Reference links: