Generating PDF documents with text-only header and/or footer

PD4PageMark header = new PD4PageMark();
      header.setAreaHeight( 20 );
      header.setTitleTemplate( "title: $[title]" );
      header.setTitleAlignment( PD4PageMark.CENTER_ALIGN );
      header.setPageNumberAlignment( PD4PageMark.LEFT_ALIGN );
      header.setPageNumberTemplate( "#$[page]" );
                 
      PD4PageMark footer = new PD4PageMark();
      footer.setAreaHeight( 30 );
      footer.setFontSize( 20 );
      footer.setColor( Color.red );
      footer.setPagesToSkip( 1 );
      footer.setTitleTemplate( "[ $[title] ]" );
      footer.setPageNumberTemplate( "page: $[page]" );
      footer.setTitleAlignment( PD4PageMark.RIGHT_ALIGN );
      footer.setPageNumberAlignment( PD4PageMark.LEFT_ALIGN );
 
      pd4ml.setPageHeader( header );
      pd4ml.setPageFooter( footer );

Converting HTML obtained from input stream to PDF

File f = new File("D:/tools/test.pdf");
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
OutputStream sos = System.out;
 
      File fz = new File("D:/tools/yahoo.htm");
      java.io.FileInputStream fis = new java.io.FileInputStream(fz);
      InputStreamReader isr = new InputStreamReader( fis, "UTF-8" );
                 
      PD4ML html = new PD4ML();
      html.setPageSize( new Dimension(450, 450) );
      html.setPageInsets( new Insets(20, 50, 10, 10) );
      html.setHtmlWidth( 750 );
      html.enableImgSplit( false );
 
      URL base = new URL( "file:D:/tools/" );
 
      // alternatively base can be specified with <base href="..."> tag
      html.render( isr, fos, base );

Converting HTML addressed by URL to PDF

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 horizontal 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. Preparing 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.

PD4ML JSP taglib and PD4ML Web Installation

As a start point for your PDF-enabled Web application development you can use the example Web application, supplied with the PD4ML Pro and PD4ML Web distributions.

Copy taglib/ directory and all its sub-content into your working directory.

Than copy pd4ml.jar (or pd4ml_demo.jar) to WEB-INF/lib directory of the webapp. Make sure, that pd4ml_tl.jar (or pd4ml_tl_demo.jar) and ss_css2.jar are already there.

Now deploy the application to your JSP runtime engine. The operation is usually differs for the engines from different vendors. Check the documentation of your version.

The following are possible steps to deploy the application to Tomcat 4.1.30 server.

  • Create pd4ml.xml file in the [tomcat_root_dir]/webapps/ with the content like below:

    <Context path=”/pd4ml docBase=”D:/tools/web debug=”0 privileged=”true reloadable=”true“>

    <ResourceLink name=”users global=”UserDatabase type=”org.apache.catalina.UserDatabase” />

    </Context>

  • Change docBase attribute to correspond your web application location
  • Restart Tomcat
  • Access the application with an URL like that http://localhost:8080/pd4ml/ (Change host and port to match your Tomcat installation). taglib/index.jsp page is be activated by default and example PDF should be generated. See: Using PD4ML custom tags in JSP section of this document for more info.

PD4ML Java API Installation

Installation of PD4ML API is straightforward: the downloaded pd4ml.jar (or pd4ml_demo.jar in a case of the evaluation version) and ss_css2.jar should be included to classpath of your project.

PD4ML is intended to be used with JDK1.3.1 and above.

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) created by the application automatically.

Run a commercial copy of PD4ML as a GUI application

D:\tools>java -jar pd4ml.jar <params>

Note. Older version require the following syntax:
D:\tools>java -Xbootclasspath/a:ss_css2.jar -jar pd4ml.jar <params>
or
D:\tools>java -cp pd4ml.jar org.zefer.pd4ml.tools.PD4Browser

 

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

Run an evaluation copy of PD4ML as a GUI application

D:\tools>java -jar pd4ml_demo.jar <params>

Note. Older version require the following syntax:
D:\tools>java -Xbootclasspath/a:ss_css2.jar -jar pd4ml_demo.jar <params>
or
D:\tools>java -cp pd4ml_demo.jar org.zefer.pd4ml.tools.PD4Browser

Preparation

To run PD4ML Converter as a standalone application it is not necessary to perform any special installation procedures. Simply copy PD4ML library and CSS parser (ss_css2.jar) to your working directory and make sure, that your JRE environment (JAVA_HOME, CLASSPATH) is properly configured.

 

 

Obtaining PD4ML

PD4ML is available in 2 variants:

  • PD4ML Standard – offers basic Java API for HTML-to-PDF conversion, includes PD4ML JSP tag library.
  • PD4ML Professional – adds to PD4ML Standard more features.

PD4ML Standard contains PD4ML library pd4ml.jar (or pd4ml_demo.jar) in lib/ dir, PD4ML JSP custom tag library (pd4ml_tl.jar orpd4ml_tl_demo.jar), the library description (pd4ml.tld), PD4ML Browser/Converter (part of the main PD4ML library) and relevant documentation.
PD4ML Pro package by content is similar to PD4ML Web, but includes more featured version of pd4ml.jar/pd4ml_demo.jar.

Both versions rely on open source CSS Parser library available from original project site (http://cssparser.sourceforge.net/) as well as from PD4ML Software download area. We recommend to use CSS Parser version patched by our team (http://pd4ml.com/cssparser-0.9.4.patched.src.2010.zip), which supports underscores in CSS properties, resolves a number of minor bugs and provides more informative error messaging.