Convert HTML to PDF in PHP with Dompdf

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
[cc-code]


PHP Hurdles

Convert To PDF

phphurdles

PHP Hurdles

PHP Hurdles is a initiative to support new developers in the field to programming and help them in challenges they face.

Learn PHP, MySQL, JavaScript, jQuery, Ajax, WordPress, Drupal, CodeIgniter, CakePHP, Web Development with PHP Hurdles tutorials. View live demo and download scripts.



[/cc]

Now just pass the index.html to dompdf in pdf.php and generate pdf of index.html
[cc-code]
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));
?>

[/cc]

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.

    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!

    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!

      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.

    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

      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.

    June 1, 2017
    Reply
    • Deepak Shah said:

      thankyou for the suggestion we will tryto fix it soon

      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!

    June 1, 2017
    Reply

Leave a Reply to health problems Cancel reply

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