Get dompdf

About dompdf

Documentation

Examples

Support

Please post bug reports & support questions to Google Groups.

Frequently Asked Questions

  1. Is there a 'hello world' script for dompdf?
  2. How do I save a PDF to disk?
  3. I have a big table and it's broken!
  4. Is there a way to add headers and footers or page numbers?
  5. How can I add an image to a header or footer on every page?
  6. How do I insert page breaks?
  7. How can I make PDFs open in the browser window instead of opening the download dialog?
  8. How do I centre a table, paragraph or div?
  9. How can I print the euro symbol?
  10. I'm getting the following error:
    Cannot access undefined property for object with overloaded property access in /var/www/dompdf/include/frame_tree.cls.php on line 160
  11. I'm getting the following error:
    Fatal error: DOMPDF_autoload() [function.require]: Failed opening required '/var/www/dompdf/include/domdocument.cls.php' (include_path='.:') in /var/www/dompdf/dompdf_config.inc.php on line 146
  12. I'm getting the following error:
    Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/dompdf/dompdf.php on line XXX
  13. I'm getting the following error:
    Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'No block-level parent found. Not good.' in C:\Program Files\Apache\htdocs\dompdf\include\inline_positioner.cls.php:68 ...
 

Is there a 'hello world' script for dompdf?

Here's a hello world script:

<?php
require_once("dompdf_config.inc.php");
$html =
    '<html><body>'.
    '<p>Hello World!</p>'.
    '</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();
$dompdf->stream("hello_world.pdf");

?>

Put this script in the same directory as dompdf_config.inc.php. You'll have to change the paths in dompdf_config.inc.php to match your installation.

[back to top]
 

How do I save a PDF to disk?

If you are using the included dompdf.php script you can pass it the "save_file" option in conjunction with the "output_file" option.

If you are using the DOMPDF class, you can save the generated PDF by calling $dompdf->output():

require_once("dompdf_config.inc.php");
$html =
    '<html><body>'.
    '<p>Foo</p>'.
    '</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();

// The next call will store the entire PDF as a string in $pdf

$pdf = $dompdf->output();

// You can now write $pdf to disk, store it in a database or stream it
// to the client.

file_put_contents("saved_pdf.pdf", $pdf);

Note that typically dompdf->stream() and dompdf->output() are mutually exclusive.

[back to top]
 

I have a big table and it's broken!

This is fixed in versions 0.4 and higher. Previous versions did not support tables that spanned pages.

[back to top]
 

Is there a way to add headers and footers?

Yes, you can add headers and footers using inline PHP. Headers and footers are added by accessing the PDF renderer directly using inline PHP embedded in your HTML file. This is similar to creating PDFs with FPDF or ezPDF from R&OS, in that you can draw lines, boxes and text directly on the PDF. Here are step by step instructions:

  1. Somewhere in your html file, near the top, open a script tag with a "text/php" type:
      <script type="text/php">
    
  2. Check if the $pdf variable is set. dompdf sets this variable when evaluating embedded PHP.
      <script type="text/php">
    
      if ( isset($pdf) ) {
    
  3. Pick a font:
      <script type="text/php">
    
      if ( isset($pdf) ) {
    
        $font = Font_Metrics::get_font("verdana", "bold");
    
  4. Use the CPDF_Adapter::page_text() method to set text that will be displayed on every page:
      <script type="text/php">
    
      if ( isset($pdf) ) {
    
        $font = Font_Metrics::get_font("verdana", "bold");
        $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
    
      }
      </script>
    
    In this example, the text will be displayed 72pt (1 in) from the left edge of the page and 18pt (1/4 in) from the top of the page, in 6pt font. {PAGE_NUM} and {PAGE_COUNT} are automatically replaced by dompdf to the appropriate values on each page. The last argument to page_text() is the colour which takes an array of the form array(r,g,b) where each of r, g, and b are between 0.0 and 1.0.
  5. There are several other methods available. See the API documentation for the CPDF_Adapter class (http://www.digitaljunkies.ca/dompdf/doc) for more details. Also have a look at the demo_01.html file in the www/test/ directory. It adds a header and footer using PDF_Adapter->page_text(). It also adds text superimposed over the rendered text using a PDF 'object'. This object is added using CPDF_Adapter->add_object(). See usage.php for more info on inline PHP.
[back to top]
 

How can I add an image to a header or footer on every page?

You can add images and shapes (line, rectangles, etc.) to every page using PDF 'objects'. A PDF object captures all rendering commands as a sort of template that can then be added to multiple pages:

<script type="text/php">

if ( isset($pdf) ) {

  // Open the object: all drawing commands will
  // go to the object instead of the current page
  $footer = $pdf->open_object();

  $w = $pdf->get_width();
  $h = $pdf->get_height();

  // Draw a line along the bottom
  $y = $h - 2 * $text_height - 24;
  $pdf->line(16, $y, $w - 16, $y, $color, 1);

  // Add an initals box
  $text = "Initials:";
  $width = Font_Metrics::get_text_width($text, $font, $size);
  $pdf->text($w - 16 - $width - 38, $y, $text, $font, $size, $color);
  $pdf->rectangle($w - 16 - 36, $y - 2, 36, $text_height + 4, array(0.5,0.5,0.5), 0.5);

  // Add a logo
  $img_w = 2 * 72; // 2 inches, in points
  $img_h = 1 * 72; // 1 inch, in points -- change these as required
  $pdf->image("/var/www/images/print_logo.png", ($w - $img_w) / 2.0, $y - $img_h, $img_w, $img_h);

  // Close the object (stop capture)
  $pdf->close_object();

  // Add the object to every page. You can
  // also specify "odd" or "even"
  $pdf->add_object($footer, "all");

</script>

See the Canvas API for details on the methods available in the $pdf object in inline PHP.

[back to top]
 

How do I insert page breaks?

Page breaks can be inserted by applying the CSS properties page-break-before and page-break-after to any block level element.

[back to top]
 

How can I make PDFs open in the browser window instead of opening the download dialog?

This is controlled by the "Attachment" header sent by dompdf when it streams the PDF to the client. You can modify the headers sent by dompdf by passing additional options to the $dompdf->stream() function:

require_once("dompdf_config.inc.php");
$html =
    '<html><body>'.
    '<p>Some text</p>'.
    '</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);

$dompdf->render();
$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));

See the class reference for full details.

[back to top]
 

How do I centre a table, paragraph or div?

You can centre any block level element (table, p, div, ul, etc.) by using margins:

<table style="margin-left: auto; margin-right: auto">
<tr>
<td> ... </td>
</tr>
</table>
[back to top]
 

How can I print the euro symbol?

You can print the euro symbol (€) using the following entity: &#0128.

[back to top]
 

I'm getting the following error:
Cannot access undefined property for object with overloaded property access in /var/www/dompdf/include/frame_tree.cls.php on line 160

This error is caused by an incompatibility with the Zend Optimizer. Disable the optimizer when using dompdf.

[back to top]
 

I'm getting the following error:
Fatal error: DOMPDF_autoload() [function.require]: Failed opening required '/var/www/dompdf/include/domdocument.cls.php' (include_path='.:') in /var/www/dompdf/dompdf_config.inc.php on line 146

This error occurs when the version of PHP that you are using does not have the DOM extension enabled. You can check which extensions are enabled by examning the output of phpinfo().

There are a couple of ways that the DOM extension could have been disabled. DOM uses libxml, so if libxml is not present on your server then the DOM extension will not work. Alternatively, if PHP was compiled with the '--disable-dom' switch or the '--disable-xml' switch, DOM support will also be removed. You can check which switches were used to compile PHP with phpinfo().

[back to top]
 

I'm getting the following error:
Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/dompdf/dompdf.php on line XXX

Nested tables are not supported yet (v0.4.3) and can cause dompdf to enter an endless loop, thus giving rise to this error.

[back to top]
 

I'm getting the following error:
Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'No block-level parent found. Not good.' in C:\Program Files\Apache\htdocs\dompdf\include\inline_positioner.cls.php:68 ...

This should be fixed in versions 0.4.1 and up. The error was caused by parse_url() thinking that the 'c' in 'c:\' was a protocol. Version 0.4.1 works around this issue.

[back to top]