HTML to PDF Library for PHP 5

DomPDF HTML to PDF As I had to build a simple html to pdf converting for a client using php, I found a beautiful library called DOMPDF written by Benj Carson.

It seemed to be very lightweight and uncomplicated, which turned out to be true. In a standard case it really is as simple as

$pdf->load_html($html);

I am going to talk about the basic usage and problems I had to solve, specially with umlauts / non latin characters.

Basic Usage

To initiate an environment you just create a new instance of the given class.

require_once('#path-to-your-installation#/dompdf/dompdf_config.inc.php');
$pdf->load_html('#some-html#');
$pdf->render();
$pdf->stream('some-file.pdf');

Further information about the basic usage of the class can be found on the project page. I am only using it inline PHP.

Convert your CSS styled HTML

Now you have to feed the method load_html with some html. Therefore we build a whole, simple page including as stylesheet (css). One of the most important things about this class is, that you simply link to a css file and dompdf parses and uses it to style your pdf.

<html>
  <head>
    <link href="some-css.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <p>Your Content</p>
  </body>
</html>

Please notice that dompdf neither supports absolute positioning nor floating. Therefor we have to use backgound-image-position to position images. Remember not to use the short-form “background” as this isn’t supported neither.

Fixing White-Spaces

I had the problem of wrong white-spaces respectively line-breaks because I had to convert a german document. tommylacroix found a solution that worked perfectly for me.
You have to go into your text_frame_decorator.cls.php around line 127 and replace

  function split_text($offset) {
    if ( $offset == 0 )
      return;
 
    $split = $this->_frame->get_node()->splitText($offset);
    $deco = $this->copy($split);

by

  function split_text($offset) {
    if ( $offset == 0 )
      return;
 
    $text = $this->_frame->get_node()->nodeValue;
 
    $split = $this->_frame->get_node()->splitText($offset);
    $this->_frame->get_node()->nodeValue = mb_substr($text,0,$offset);
    $split->nodeValue = mb_substr($text,$offset);
    $deco = $this->copy($split);

Using inline PHP embedded in HTML

Last but not least there are several more options to manipulate the rendered pdf file. By adding some inline php code in your html we can manage to display page numbers, a header or footer, watermarks, etc.

To add page numbers to your pdf, we add the following code to our html:
(I explained the usage in comments)

  <script type="text/php">
    if(isset($pdf)) {
      ## Setup a tiny configuration ##
      $text_height = 17;
      $text_size = 10;
      $font = Font_Metrics::get_font("helvetica", "normal");
 
      ## Get the dimensions of our pdf-file ##
      $dimensions = array("width" => $pdf->get_width(), "height" => $pdf->get_height());
 
      ## The Counter-Text with variables: ##
      $pageCounterText = "Page: {PAGE_NUM} of {PAGE_COUNT}";
 
      ## If you want to position the Counter on the right, you need the text-width: ##
      $pageCounterWidth = Font_Metrics::get_text_width($pageCounterText, $font, $text_size);
 
      ## Not we can add this to our pdf:
      $pdf->page_text(10, ($dimensions["height"]-$text_height-20), $pageCounterText, $font, $text_size, array(0,0,0));
    }
  </script>

The page_text method works like this:

  page_text(float $x, float $y, string $text, string $font, float $size, array $color = array(0,0,0))
 
    ## Explanation: ##
    $x: Position from the top
    $y: Position from the left
    $size: Font-Size
    $color: an rgb-array -> array(R, G, B); (optional)
  1. hanno says:

    great, thanks for this interesting blogpost. greetings to your client using php. :-)

Leave a Reply
  1. (required)
  2. (will not be published) (required)