HTML to PDF converter for Java and .NET


Creating PDF documents from PHP with PD4ML

(updated August 07, 2008)

PD4ML is HTML-to-PDF converting Java library, but with a simple wrapper application it works also in PHP environment. The application expects there is Java runtime environment 1.4 (or above) installed on the server.

Below is a sample implementation of "As PDF" button, which can add to any PHP page its PDF view.

First we need a small Java application, which accepts a document URL as a parameter and outputs the resulting PDF to STDOUT. Download the application in source and in compiled form.

The Java code utilizes only basic features of PD4ML, but it could be easily extended. See PD4ML reference.

The next step is to create a converter PHP (let's call it pd4ml.php).

  if ($_SERVER["HTTPS"] != "on") {
    // MS IE needs to cache PDF obtained by HTTPS.
    header('Pragma: no-cache');
    header('Expires: -10000');

  if (array_key_exists('url', $_POST)) {

    header('Content-type: application/pdf');
    header('Content-disposition: inline');
    //header('Content-disposition: attachment; filename=test.pdf');

    // UNIX version
    // Please make sure, that web user is allowed to write to ./stderr.txt.
    // If not, please remove 2>>stderr.txt command-line suffix

    passthru('java -Xmx512m -Djava.awt.headless=true -cp .:pd4ml_demo.jar Pd4Php \'' .
                       $_POST['url'] . '\' 800 A4 2>>stderr.txt');

    // Windows version
    //passthru('java -Xmx512m -cp .;pd4ml_demo.jar Pd4Php ' . $_POST['url'] . ' 800 A4');

  } else {
    echo 'invalid usage';


The converter PHP expects the source URL is passed as an HTML form variable named 'url'.

OK, the last step is to create the button.


// the function determines the current page URL
function curPageURL() {
  $pageURL = 'http';
  if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  $pageURL .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= "localhost:".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } else {
    $pageURL .= "localhost".$_SERVER["REQUEST_URI"];
  return $pageURL;

<!-- ... your HTML code here ... -->

<!-- the button -->
<form action="pd4ml.php" method=post>
  <input type=hidden value="<?php echo curPageURL(); ?>" name=url>
  <input style="pd4ml-display: none; pd4ml-visibility: hidden"
    type=submit value="Get the invoice as PDF">

Useful info:

  • In order to deploy the PDF generating solution you need to copy pd4ml.jar (or pd4ml_demo.jar) and ss_css2.jar from PD4ML distribution, the wrapper class Pd4Php.class and pd4ml.php file to any PHP-enabled directory of your web server. Please also make sure that your PHP engine is not in safe mode, which disables passthru() calls (can be checked with <?php phpinfo()?>).
  • PD4ML-specific style="pd4ml-display: none; pd4ml-visibility: hidden" excludes the button from the resulting PDF layout.
  • pd4ml.php script assumes, java runtime location is in PATH of your server. If not, you need to explicitly add path to the executable, i.e.: passthru('/usr/local/jdk1.4/bin/java ...
  • If you deploy the application to a server, which hosts multiple web sites, "localhost" server address cannot be distinct. Please use your actual server name in curPageURL() instead of it.
  • PD4ML Pro feature: in order to add national characters to the resulting PDF, copy TTF fonts, which have the needed chars to fonts/ directory (relative to pd4ml.php location) and trigger http://yourserver/path/indexfonts.php. After that you need to explicitly address the font face names from your HTML/PHP (either via <font face=""> or via "font-family" CSS property). And one more important thing is to add '-ttf ./fonts' Pd4Php command-line parameter.
  • There are two places, where you can control PDF page margins. Default HTML document margins can be changed with BODY { margin: 0 } CSS property. PDF margins (insets) can be impacted via Pd4Php command line parameter '-insets TOP,LEFT,BOTTOM,RIGHT,units'. For example: -insets 10,20,10,10,mm
  • Page orientation can be changed from the default PORTRAIT with -orientation LANDSCAPE command line parameter
  • Pd4Php command-line syntax: Pd4Php '<url>' <htmlWidth> [pageFormat] [-permissions <NUMBER>] [-bookmarks <HEADINGS|ANCHORS>] [-orientation <PORTRAIT|LANDSCAPE>] [-insets <T,L,B,R,><mm|pt>] [-ttf <ttf_fonts_dir>]
  • PD4ML Pro feature: you can define page headers/footers with HTML code like the following
    <pd4ml:page.header><img src="logo.gif"> page $[page] of $[total]</pd4ml:page.header>
  • If you need a generated PDF not to be sent to clients browser, but saved to the disk, add ' >/generated/file/location/file.pdf' command-line redirect and remove header('Content-type: application/pdf'); directive in pd4ml.php.

Download all the files mentioned in the article and a demo invoice PHP (index.php), convertable to PDF.

List of HTML tags supported by PD4ML
List of CSS properties supported by PD4ML



Copyright ©2004-17 zefer|org. All rights reserved. Bookmark and Share