How to use dompdf

In this guide I’m going to show you how you can generate pdf files in php. Pdf files can be use to store reports for easy printing. This is useful when creating a system which needs to generate reports.

In the time of writing of this guide, I’m using dompdf. It’s an html to pdf converter. Its easy to generate pdf files using php and dompdf. Since php, generates html files when parse. And dompdf, converts html files to pdf.




Let’s get started. First you have to extract the zip file that you have downloaded from where dompdf is hosted.

It should look like this when extracted:


Next thing that you have to do is to create a new php file. This file would call dompdf, and it will generate a pdf file from an html file.

Place this piece of code in the beginning of your file:


I had trouble using absolute urls. So you might as well use a relative url when including the configuration file of dompdf. Like the one above.

Then declare a new DomPdf object. Always remember that there should only be one object. And it should be declared before you start using codes which are associated with dompdf. If you want to create 2 or more objects, then be sure to name it differently:

$dompdf = new DOMPDF();

The following example is taken from the wiki of dompdf, check it out for more details about dompdf.

$html =
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
$dompdf = new DOMPDF(); 

The code is self explanatory since the function names actually say what they will do. That particular example will generate a pdf file named sample.pdf. And it gets the data to be placed in that pdf from the ‘$html’ php variable.

In this second example, we will be using a php function ‘ob_start’. Which will generate html files. But not just generate, it will also save it to a directory. And that directory can be used to generate pdf files from html files which are saved on the hard disk.

Begin by,  calling the ob_start function. Remember that you must place this directly  before the codes which when parsed produces an html file.

<?php ob_start(); ?>
<table border="1">
<td>Table Definition</td>

Examine the example carefully. You must remember that your code should comply with html standards. Because if not, dompdf won’t work and you’ll run into some errors. That’s what happened to me when I first used dompdf. Thankfully I was able to figure things out. That’s why I’m here talking about how to use it. Along with some tips to remember so that you won’t run into the same errors that I got. The html validator will be handy. So try to use it.

Next thing that you need to do is to actually put the contents that the function has gathered into an html file:

file_put_contents('../tmp/customerpay.html', ob_get_contents());

All you have to do now is to actually call dompdf to generate pdf from the html file that you have just created:


I’ll leave to you the challenge of supplying the filename for the pdf file which will be generated. If you don’t want the pdf files to have the same filename, then use your creativity on what to put on that $ofilename variable.



That’s a short guide on how you can use dompdf to generate pdf files on php.  If  this guide helped you, I’d appreciate it if you would share this tutorial using the buttons below.

32 thoughts on “How to use dompdf

  1. I keep getting this same damn error:

    Fatal error: Uncaught exception ‘DOMPDF_Exception’ with message ‘An input file is required (i.e. input_file _GET variable).

    Ugh. About to give up.

    • I got that error before, when I mindlessly included dompdf.php instead of
      Also make sure that the files that you need to include are referenced correctly.

  2. i want to known something more about dompdf..bcz i have no experience on working with dompdf so plzz tell me more about dompdf…..fahim

  3. can i ask an example for generating pdf from php-mysql query… im having a problem outputing the records in html 🙂 thanks in advance

  4. First of all thx for the explanations.

    Im having a problem rendering the table somehow the border in the collums is not showing at all only lines, you know what the problem might be?

    Thx in advance 🙂

    • Is the whole content of the database being outputted in html?
      And once you print it as pdf only the first record shows up?
      Here’s the template:

      //your table here
      $_SESSION[‘prints’] = ob_get_contents();

      $dompdf->stream(“somefile.pdf”, array(“Attachment” => 0));

      Paste your code here so that I can see the problem.

  5. Fantastic tutorial, not many out there on dompdf so was very happy to come across this.

    Can I convert to PDF only part of a PHP file using dompdf? I’d like to skip navigation links generated at the top of the php file.

  6. hello, how do i display data from the sql table using while loop iusing dompdf..i have been trying a lot of tutorial for three days now but i still have not get the result.

    //here’s the logic.

    $html =”

    //how to include the while loop here because
    if I put the while loop inside “..” it causes error.
    i want to display the names from the database in the next row..


    please help.

  7. to view the value in variable..the variable is putting in table.. for example


    :$row[‘name’]//select from database

    I/C Number
    :$row[‘name’]//select from database

    hope you can help me….

  8. A little confused your tutorial coz, you don´t explain if it´s on the same file (the example 2). anyway, your last link was useful to directly insert php script within the html.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s