There are many tools on the internet that allow you to convert files of various formats to PDF – but sometimes you need to make such conversions using your code. In this article, we’ll specifically discuss how to convert an HTML file or content to a PDF file using PHP 5.
For the task at hand, we’ll be using Dompdf – a CSS2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer – it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
- Download the latest stable version of Dompdf, additionally you can also download the detailed documentation if needed.
- Untar/unzip the source package in a directory accessible by your webserver; for example: “wwwdompdf”
- Edit dompdf_config.inc.php to fit varous parameters for your installation; generally you should not need to change anything and it should work with default settings.
- Give your webserver write permission on the path specified in DOMPDF_FONT_DIR (lib/fonts by default). Under *nix, ideally you can make the webserver group the owner of this directory and give the directory group write permissions. For example, on Debian systems, apache runs as the www-data user:
$ chgrp www-data lib/fonts
$ chmod g+w lib/fonts
If your user is not a member of the www-data group or you do not have root priviledges, you can make the directory world writable and set the sticky bit:
$ chmod 1777 lib/fonts
Invoking dompdf via the web
The dompdf.php script receives input parameters via $_GET and can stream a PDF directly to the browser. dompdf.php accepts the following $_GET variables:
input_file required a rawurlencoded() path to the HTML file to process. Remote files (http/ftp) are supported if fopen wrappers are enabled. paper optional the paper size. Defaults to ‘letter’ (unless the default has been changed in dompdf_config.inc.php). See include/pdf_adapter.cls.php, or invoke dompdf.php on the command line with the -l switch for accepted paper sizes. orientation optional ‘portrait’ or ‘landscape’. Defaults to ‘portrait’. base_path optional the base path to use when resolving relative links (images or CSS files). Defaults to the directory containing the file being accessed. (This option is useful for pointing dompdf at your CSS files even though the HTML file may be elsewhere.) output_file optional the rawurlencoded() name of the output file. Defaults to ‘dompdf_out.pdf’. save_file optional If present (i.e. isset($_GET[“save_file”]) == true’);), output_file is saved locally, Otherwise the file is streamed directly to the client.
$url = “dompdf.php?input_file=” . rawurlencode(“path_to_the_file_to_be_converted.html”) . “&paper=letter&output_file=” . rawurlencode(“my_output_filename.pdf”);
header(“Location: http://” . $_SERVER[“HTTP_HOST”] . “/$url”);
Using the dompdf class directly
Using the dompdf class directly is fairly straightforward:
$html = “<html><body>” . “<p>Put your html here, or generate it with your favourite ” . “templating system.</p>” . “</body></html>”;
$dompdf = new DOMPDF();
For complete details, see the Usage instructions and API documentation for the class interface definition.