How to export to a PDF using DOMPDF with PHP

Home > Search > How-to
  by

DOMPDF is software written in PHP which allows you to export HTML pages to PDF. Follow these instructions to download and install DOMPDF. In this tutorial, I will explain how to use DOMPDF on a Linux Web server.

Because of the many dependencies needed by DOMPDF, I usually recommend using other options, such as HTML2PS. HTML2PS is very easy to installl on Linux, using apt-get install or yum install. On the other hand, following is a list of dependencies needed by DOMPDF. HTML2PS also has better support for CSS.

  • PHP version 5 or higher
  • php-mbstring (not available using apt-get install or yum install; download rpm)
  • php-common
  • lib.so.6() (64bit)
  • lib.so.6(GLIB_2.2.5) (64bit)
  • lib.so.6(GLIB_2.3.4) (64bit)
  • lib.so.6(GLIB_2.3) (64bit)
  • lib.so.6(GLIB_2.4) (64bit)
  • libpthread.so.0() (64bit)
  • rtld(GNU_HASH)
  • DOM

In order to use DOMPDF, you need to be running PHP version 5 or higher. To determine the version of PHP being run on your Web server, go to http://www.yourdomain.com/phpinfo.php. You also will need the mbstring extension and the DOM extension. 

  1. Download the DOMPDF package from https://github.com/dompdf/dompdf/releases. Because this tutorial is for Linux, we would download the .tar.gz package.
  2. Extract the contents of the tar gunzip using this command:
tar -zxvf dompdf-version.tar.gz -C /path/to/web/server/root
  1. Remove the origial tar gunzip file.
rm dompdf-version.tar.gz
  1. Add the followiing markup to the PHP page you want to add the export to PDF option.
<form method="post" action="stage.php">
  <textarea name="content" id="content"></textarea>
  <br />
  <input type="submit" name="submit" id="submit" value="create pdf">
</form>

<?php
include ('dompdf-version/dompdf_config.inc.php');

if(isset($_POST['submit'])) 
{
  $content = $_POST['content'];
  $dompdf = new DOMPDF();
  $dompdf->load_html($content);
  $dompdf->render();
  $dompdf->stream('example.pdf');
}
?>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments