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]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.
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()
.
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]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.
This is fixed in versions 0.4 and higher. Previous versions did not support tables that spanned pages.
[back to top]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:
<script type="text/php">
<script type="text/php"> if ( isset($pdf) ) {
<script type="text/php"> if ( isset($pdf) ) { $font = Font_Metrics::get_font("verdana", "bold");
<script type="text/php"> if ( isset($pdf) ) { $font = Font_Metrics::get_font("verdana", "bold"); $pdf->page_text(72, 18, "Fancy Header", $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. 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.
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]This error is caused by an incompatibility with the Zend Optimizer. Disable the optimizer when using dompdf.
[back to top]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(); $domper->stream("my_pdf.pdf", array("Attachment" => 0));
See the class reference for full details.
[back to top]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]