Convert HTML to PDF in PHP with Dompdf

No votes yet.
Please wait...

Sometimes we need to convert a html file into a downlodable or readable format for use. So today PHP Hurdles is here with a tutorial where we will Convert HTML to Pdf in PHP Using DOMpdf.At the time of download web page content as PDF file, we needed to convert HTML to PDF. This tutorial shows the easiest way for convert HTML to PDF using PHP.

Dompdf is a PHP library that used to convert HTML to PDF file. Using Dompdf library files you can easily implement the generate PDF feature in the web application. Here we’ll provide the simple script to Convert HTML to PDF in PHP with Dompdf.

So lets start with the tutorial

Firstly,

Installation & Setup

Download stable release of dompdf from GitHub . Extract the downloaded dompdf package and rename it with “dompdf”.

Download php-font-lib from GitHub, extract it and place the entire folder into the dompdf/lib/ directory.

Download php-svg-lib from GitHub, extract it and place the entire folder into the dompdf/lib/ directory.

Now Create a index.html file which will have a convert to pdf link and our content to be converted into pdf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<title>PHP Hurdles</title>
<html>
<body>

<div class="div-center">
            <a href="pdf.php" target="_blank" class="pdfLink">Convert To PDF</a>
           


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#eeeeee" class="tbl" style="width:100%">
<tbody>
    <tr>
        <td>
            <table width="690" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="#eeeeee">
            <tbody>
                <tr>
                    <td colspan="3" height="80" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="#eeeeee" style="padding:0;margin:0;font-size:0;line-height:0">
                        <table width="690" align="center" border="0" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr>
                                <td width="30"></td>
                                <td align="left" valign="middle" style="padding:0;margin:0;font-size:0;line-height:0"><a href="http://www.phphurdles.com/" target="_blank"><img src="hurdles.png" alt="phphurdles" width="200" height="50"></a></td>
                                <td width="30"></td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="3" align="center">
                        <table width="630" align="center" border="0" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr>
                                <td colspan="3" height="60"></td></tr><tr><td width="25"></td>
                                <td align="center">
                                    <h1 style="font-family:HelveticaNeue-Light,arial,sans-serif;font-size:48px;color:#404040;line-height:48px;font-weight:bold;margin:0;padding:0">PHP Hurdles</h1>
                                </td>
                                <td width="25"></td>
                            </tr>
                            <tr>
                                <td colspan="3" height="40"></td></tr><tr><td colspan="5" align="center">
                                    <p style="color:#404040;font-size:16px;line-height:24px;font-weight:lighter;padding:0;margin:0">PHP Hurdles is a initiative to support new developers in the field to programming and help them in challenges they face.</p><br>
                                    <p style="color:#404040;font-size:16px;line-height:22px;font-weight:lighter;padding:0;margin:0">
    Learn PHP, MySQL, JavaScript, jQuery, Ajax, WordPress, Drupal, CodeIgniter, CakePHP, Web Development with PHP Hurdles tutorials. View live demo and download scripts.</p>
                                </td>
                            </tr>
                            <tr>
                            <td colspan="4">
                                <div style="width:100%;text-align:center;margin:30px 0">
                                    <table align="center" cellpadding="0" cellspacing="0" style="font-family:HelveticaNeue-Light,Arial,sans-serif;margin:0 auto;padding:0">
                                    <tbody>
                                        <tr>
                                            <td align="center" style="margin:0;text-align:center"><a href="http://www.phphurdles.com/" style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;letter-spacing:1.2px" target="_blank">Visit website!</a></td>
                                        </tr>
                                    </tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                        <tr><td colspan="3" height="30"></td></tr>
                    </tbody>
                    </table>
                </td>
            </tr>
            </tbody>
            </table>
        </td>
    </tr>
</tbody>
</table>

            </div>
</body>
</html>

Now just pass the index.html to dompdf in pdf.php and generate pdf of index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$html = file_get_contents("index.html");
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codexworld",array("Attachment"=>0));
?>

This is all we have to do to convert HTML to pdf using PHP with DOMpdf.
For demo Download Here

If you have any query about this tutorial and scripts, feel free to comment us.

Hardwork doesn’t guarentee success but improves its chances so keep working hard
because Hurdles aren’t really hurdles

9 Comments

  1. Developper said:

    Hi Guys…!!
    it is working for your html file only. On others urls i have tried ,, some times it shows warning like,
    “Fatal error: Using $this when not in object context in /home/appuonline/public_html/appuonline/test/dompdf/dompdf/src/FrameReflower/Text.php(452) : runtime-created function on line 1″

    Sometimes it shows
    ” Something happened to our end”
    And when i tried “www.appuonline.com” it shows pdf not like html original contents.

    No votes yet.
    Please wait...
    April 18, 2017
    Reply
  2. online job posting websites said:

    Hey there this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

    No votes yet.
    Please wait...
    May 31, 2017
    Reply
    • Saurabh Dubey said:

      Hey, It depends on which platform you are choosing for your blog if you are not going with any CMS You can use TinyMCE or CKEditor for the text box to post the blog content.

      Hope it helps.

      Cheers!

      No votes yet.
      Please wait...
      May 31, 2017
      Reply
  3. Because the admin of this site is working, no doubt very rapidly it will be famous, due to its quality contents.

    No votes yet.
    Please wait...
    June 1, 2017
    Reply
    • Deepak Shah said:

      thankyou so much it means a lot . and more than being famous it would be a great pleasure if we can help more and more developers
      thankyou for the appreciation

      No votes yet.
      Please wait...
      June 2, 2017
      Reply
  4. Hello, Neat post. There’s an issue along with your site in web explorer, may check this? IE nonetheless is the marketplace leader and a big component to other folks will leave out your wonderful writing because of this problem.

    No votes yet.
    Please wait...
    June 1, 2017
    Reply
    • Deepak Shah said:

      thankyou for the suggestion we will tryto fix it soon

      No votes yet.
      Please wait...
      June 2, 2017
      Reply
  5. Hiya! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My website goes over a lot of the same topics as yours and I feel we could greatly benefit from each other. If you might be interested feel free to send me an e-mail. I look forward to hearing from you! Awesome blog by the way!

    No votes yet.
    Please wait...
    June 1, 2017
    Reply
    • Deepak Shah said:

      mail me with all ellaboration at shahdeepak88@gmail.com or sdsrbh5@gmail.com.
      Thankyou

      Rating: 4.0. From 1 vote.
      Please wait...
      June 2, 2017
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
10 + 21 =