HTML to PDF converter for Java and .NET

HOME   FEATURES   PRODUCTS   DOWNLOADS   BUY NOW!   SUPPORT

 

NEW:
PD4ML v4.x Web site
PD4ML v4.x examples repository

 

PD4ML v3 Usage Examples

1. PD4ML API calls in your Java application

1  import org.zefer.pd4ml.PD4ML;
   import org.zefer.pd4ml.PD4Constants;

    ...

 2  protected Dimension format = PD4Constants.A4;

   protected boolean landscapeValue = false;

   protected int topValue = 10;

   protected int leftValue = 10;

   protected int rightValue = 10;

   protected int bottomValue = 10;

   protected String unitsValue = "mm";

   protected String proxyHost = "";

   protected int proxyPort = 0;

 

3  protected int userSpaceWidth = 780;

 

   ...

 

   private void runConverter(String urlstring, File output) throws IOException {

 

         if (urlstring.length() > 0) {

                if (!urlstring.startsWith("http://") && !urlstring.startsWith("file:")) {

                              urlstring = "http://" + urlstring;

                }

 

4               java.io.FileOutputStream fos = new java.io.FileOutputStream(output);

               

5               if ( proxyHost != null && proxyHost.length() != 0 && proxyPort != 0 ) {

                       System.getProperties().setProperty("proxySet", "true");

                       System.getProperties().setProperty("proxyHost", proxyHost);

                       System.getProperties().setProperty("proxyPort", "" + proxyPort);

                }

 

6               PD4ML pd4ml = new PD4ML();

 

7               try {                                                              

                       pd4ml.setPageSize( landscapeValue ? pd4ml.changePageOrientation( format ): format );

                    } catch (Exception e) {

                       e.printStackTrace();

                    }

                      

                if ( unitsValue.equals("mm") ) {

                       pd4ml.setPageInsetsMM( new Insets(topValue, leftValue,

bottomValue, rightValue) );

                } else {

                       pd4ml.setPageInsets( new Insets(topValue, leftValue,

bottomValue, rightValue) );

                }

 

                pd4ml.setHtmlWidth( userSpaceWidth );

               

8               pd4ml.render( urlstring, fos );

         }

   }

 

       ...

 

 

Comments:
1. Import the PD4ML converter class
2. Define HTML-to-PDF converting parameter values if needed. See API reference for more info.
3. Specify user space width. It has an analogy to Web-browser window horizonltal size. From common web-browsing experience you can guess, that changing of the size can affect the HTML document representation: HTML elements arrangement, vertical size etc. See API reference for more info.
4. Perparing output stream for PDF generation.
5. Specifying proxy settings if the source HTML document is behind the firewall.
6. Instantiating PD4ML converter.
7. Passing to it HTML-to-PDF converting parameters.
8. Performing HTML-to-PDF translation. Note: using of an URL is not mandatory. PD4ML can read a source HTML from input stream. See API reference for more info.

2. Running PD4ML converter as a standalone GUI application.
 
D:\tools>java -jar pd4ml.jar <params>

Note: The application creates and updates automatically pd4browser.properties file that holds options specified in its options dialog.

3. Running PD4ML as a command line converter tool

In order to start the application in command-line mode simply append two parameters (source URL and output file name) to the command we used above.

D:\tools>java -jar pd4ml.jar http://localhost:80/ test.pdf
or
D:\tools>java -jar pd4ml.jar file:d:/test.html test.pdf

Note: there is no possibility to pass HTML-to-PDF convert parameters in the command line. The application takes parameters from pd4browser.properties if exists.

4. Web scenario: Using PD4ML custom tags in JSP.

Note: some combinations of MS Internet Explorer and Adobe Acrobat reader plugin versions are buggy. Instead of a PDF generation result MS IE displays a blank page. For troubleshooting information and answers to general questions visit PD4ML Support Forum.

1  <%@ taglib uri="http://pd4ml.com/tlds/pd4ml/2.6" prefix="pd4ml" %><%@page
contentType=
"text/html; charset=ISO8859_1"%><pd4ml:transform

      screenWidth="400"

      pageFormat="A5"

      pageOrientation="landscape"

      pageInsets="100,100,100,100,points">

 

  <html>

      <head>

            <title>pd4ml test</title>

            <style type="text/css">

                  body {

                        color: red;

                        background-color: #FFFFFF;

                        font-family: Tahoma, "Sans-Serif";

                        font-size: 10pt;

                  }

            </style>

      </head>

      <body>

2           <img src="images/logos.gif" width="125" height="74">

            <p>

            Hello, World!

3 <pd4ml:page.break/>

            <table width="100%" style="background-color: #f4f4f4; color: #000000">

            <tr>

            <td>

                  Hello, New Page!

            </td>

            </tr>

            </table>

      </body>

  </html>

4 </pd4ml:transform>

 

Comments:
1. PD4ML JSP taglib declaration. Make sure there are no whitespace characters before <pd4ml:transform>. JSP content nested to with <pd4ml:transform> is passed to the PD4ML converter.
2. The directive forces PD4ML converter to insert a page break to the output PDF.
3. Closing of the transformation tag. Any content that appears after the tag is ignored.

Click here to see the resulting PDF. (Tomcat 4.1.30 WinXP)

5. More examples: download.

6. Useful hints and tips in PD4ML Cookbook.

 

 

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