|
Usage
UsageUsing the dompdf class directlyUsing the dompdf class directly is fairly straightforward: <?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>Below is a summary of the methods available in the dompdf class. Method summaryThe methods available in the instances of the DOMPDF class are : load_htmlLoads an HTML string. Parse errors are stored in the global array $_dompdf_warnings. Arguments: load_html_fileLoads an HTML file. Parse errors are stored in the global array $_dompdf_warnings. Arguments: outputReturns the PDF as a string. The file will open a download dialog by default. The options parameter controls the output. Arguments: renderRenders the HTML to PDF. Arguments: none. set_base_pathSets the base path, used for external stylesheets and images. Arguments: set_paperSets the paper size & orientation Arguments: streamStreams the PDF to the client. The file will open a download dialog by default. The options parameter controls the output. Arguments: Invoking dompdf via the webThe dompdf.php script is not intended to be an interactive page. It receives input parameters via $_GET and can stream a PDF directly to the browser. This makes it possible to embed links to the script in a page that look like static PDF links, but are actually dynamically generated. This method is also useful as a redirection target. dompdf.php accepts the following $_GET variables:
One technique for generating dynamic PDFs is to generate dynamic HTML as you normally would, except instead of displaying the output to the browser, you use output buffering and write the output to a temporary file. Once this file is saved, you redirect to the dompdf.php script. If you use a templating engine like Smarty, you can simply do: <?php
$tmpfile = tempnam("/tmp", "dompdf_");
file_put_contents($tmpfile, $smarty->fetch()); // Replace $smarty->fetch()
// with your HTML string
$url = "dompdf.php?input_file=" . rawurlencode($tmpfile) .
"&paper=letter&output_file=" . rawurlencode("My Fancy PDF.pdf");
header("Location: http://" . $_SERVER["HTTP_HOST"] . "/$url");
?>If you use any stylesheets, you may need to provide the base_path option to tell dompdf where to look for them, as they are not likely relative to /tmp ;). Invoking dompdf via the command lineYou can execute dompdf.php using the following command: $ php -f dompdf.php -- [options] (If you find yourself using only the cli interface, you can add #!/usr/bin/php as the first line of dompdf.php to invoke dompdf.php directly.) dompdf.php is invoked as follows: $ ./dompdf.php [options] html_file html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' character to read from standard input.
Examples: $ php -f dompdf.php -- my_resume.html $ ./dompdf.php -b /var/www/ ./web_stuff/index.html $ echo '<html><body>Hello world!</body></html>' | ./dompdf.php - Inline PHP supportdompdf supports two varieties of inline PHP code. All PHP evaluation is controlled by the DOMPDF_ENABLE_PHP configuration option. If it is set to false, then no PHP code is executed. Otherwise, PHP is evaluated in two passes: The first pass is useful for inserting dynamic data into your PDF. You can do this by embedding <?php ?> tags in your HTML file, as you would in a normal .php file. This code is evaluated prior to parsing the HTML, so you can echo any text or markup and it will appear in the rendered PDF. The second pass is useful for performing drawing operations on the underlying PDF class directly. You can do this by embedding PHP code within <script type="text/php"> </script> tags. This code is evaluated during the rendering phase and you have access to a few internal objects and operations. In particular, the $pdf variable is the current instance of Canvas. Using this object, you can write and draw directly on the current page. Using the Canvas::open_object(), Canvas::close_object() and Canvas::add_object() methods, you can create text and drawing objects that appear on every page of your PDF (useful for headers & footers). The following variables are defined for you during the second pass of PHP execution:
For more complete documentation of the Canvas API, see the API documentation. | |||||||||||||||||||||||||||||||||||||||||
Thanks for The Code......Gr8 job .....
http://www.digitaljunkies.ca site is not available long time and archive with dompdf does not contain offline documentation as well as no other places in the internet to see docs. It's really bad that you leave such good library without any support or backup. :(
You can find a copy of the original documentation at http://eclecticgeek.com/dompdf/docs/
This documentation is from the 0.5.1 release, which is the most current available at present. We'll update the documentation when we get closer to a final version of 0.6.0.
Where we can get the doc for the api ? I want to add a simple footer :(
Samples of creating headers/footers are available on the FAQ. Most but not all the available functions available are shown under the questions "Is there a way to add headers and footers or page numbers?" and "How can I add an image to a header or footer on every page?" http://code.google.com/p/dompdf/wiki/FAQ
Currently you can only find the full API documentation on digitaljunkies.ca. Until Benj's site comes back up it's not available elsewhere. I believe the API documentation was created using PHPDocumentor so when I get a chance I'll see about regenerating it and posting it to eclecticgeek.com. However, it should be noted that the functionality available to inline scripts is a subset of the full API, and you can't access the PDF functions through the object directly.
When we're ready to release the next revision we'll add a more detailed page that covers the capabilities available with inline scripts.
When will digitaljunkies.ca come back? This is redicilous...
@vanleerdamwebsolutions: It is my understanding the Benj has less time to devote to this project than he used to. As such you should consider the documentation here to be the official documentation. The information available on this site will be the most up-to-date as DOMPDF is developed further. Everything that was available on digitaljunkies.ca can either be found here or, in the case of the usage and api documentation, on my site: http://eclecticgeek.com/dompdf/docs/
If there's any other information you need please post a query to the support group: http://groups.google.com/group/dompdf
Great job :) Big THX!!
Hay, great sw! How is it possible to add background to page (image or other PDF) ? Can you elaborate on font adding (Tahoma, for example), or do some php cli tool to help with adding TTF fonts?
@saso.filipovic: Can you post your questions to the support group? We'll do our best to answer you there. http://groups.google.com/group/dompdf
Have any solution for japanese text? All i got is weird characters instead of Japanese text on output...
@lastboyofsiberspace: Working with non-ASCII characters can be tricky. You need a font that supports your characters and DOMPDF 0.6.0. Post your question to the support group and we'll do our best to answer you there. http://groups.google.com/group/dompdf
fab work ....
I'm trying to add IMAGE to footer and then TEXT placed over that image. The problem is that the text is hidden behind the image. Is there a way to bring the text to the front? Thanks.
@pdolinaj: This may be due to the order the content is placed in the document. If you haven't already, please ask your question on the support group: http://groups.google.com/group/dompdf
Wow... amazing script! Thanks for your work. Can you provide some comments / docs to your 'Pages' example? Not sure what you're doing with the js width and height in that example. I have about 80 html tables (user profiles) that I would like to put in one giant php string and output to pdf; having each table on it's own page.. so the final product would be an 80 page pdf. Ideas?
@jamesmehorter you'll get a better response from the support forum. The short answer is to add a page break in your HTML before each table, e.g.:
<div style="page-break-before: always;"></div>
Great project! I'm using it on a project to generate contracts in pdf format, it works perfect! Now I would like automatic save the file before it prompt to me, is that possible?
@DeepCell? use $dompdf->output() in conjunction with file_put_contents() to save the file, then call $dompdf->stream() to send it off to the web browser. Or, if you're saving to a web accessible directory, point to the saved file rather than use $dompdf->stream()
Very nice! I've started using R&OS class, but needed to translate html tags. ¿Is there any way to just get the translated HTML to \"pdf language\"? I'm doing some formating using R&OS methods like setColor() and filledRectangle().
Thanks in advice.
@leifont not sure what you mean. Can you explain more over at the support forum: https://groups.google.com/forum/#!forum/dompdf
How can I add a button to convert a current web page to PDF, is that easy?
@mahmoudzadah it can be as simple or complex as you want to make it. Post your question on the support forum and we'll see if we can help you out: https://groups.google.com/forum/#!forum/dompdf
How Can I add page header in each page saying "PAGE NUMBER/PAGE COUNT"? although I have tried following code but it does not work:
// $dompdf = new DOMPDF();
$canvas = $dompdf->get_canvas(); $font = Font_Metrics?::get_font("helvetica", "bold");
// the same call as in my previous example $canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
$dompdf->load_html($POST["html"]); $dompdf->set_paper($POST["paper"], $POST["orientation"]); $dompdf->render();
$dompdf->stream("dompdf_out.pdf"); //
The above code give me Error "Fatal error: Call to a member function page_text() on a non-object"
Anybody please help!
Thanks Ashu
@ashusaxena.mca script to generate a header should be included as part of the body of the HTML document.
Re-post your question on the support forum and we'll see if we can help you out: http://groups.google.com/group/dompdf
Installed. All images with absolute paths get red x's. Tried a million things. No matter what, I get red xs instead of images.
@drew.borland if you need help please post to the support forum: http://groups.google.com/group/dompdf
Can some one tell me if domPDF supports PDF templates? I want to load a PDF file as template and fill the empty places using X,Y coordinates. This is helpful when I need to fill a form and I don't want to waste time in making a ditto copy of that PDF form in HTML. I'll be thankful if some gives a sample code along with explanation. Thanx.
Use "ini_set('magic_quotes_runtime', 0);" in place of set_magic_quotes_runtime(0); and "ini_set('magic_quotes_runtime', $tmp);" in place of set_magic_quotes_runtime($tmp); as set_magic_quotes_runtime() is deprecated w.e.f php 5.3.0 in lib/class.pdf.php
images in beta 6:
http://code.google.com/p/dompdf/issues/detail?id=462
Thang You Very much
Page numeration using inline php,
requirements are:
you could use a simple inline:
if ( isset($pdf) ) { $font = Font_Metrics::get_font("yourfont", "normal"); $size = 9; $y = $pdf->get_height() - 24; $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width("1/1", $font, $size); $pdf->page_text($x, $y, "{PAGE_NUM}/{PAGE_COUNT}", $font, $size); }But this does not met any of the requirements:
this because PAGE_NUM and PAGE_COUNT tokens can be used only in the page_text method
here the solution to all my requirements:
if ( isset($pdf) ) { $pdf->page_script(' if ($PAGE_COUNT > 1) { $font = Font_Metrics::get_font("yourfont", "normal"); $size = 9; $pageText = $PAGE_NUM . "/" . $PAGE_COUNT; $y = $pdf->get_height() - 24; $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width($pageText, $font, $size); $pdf->text($x, $y, $pageText, $font, $size); } '); }Now I can customize without problems my page number string, and is always displayed in the right way.