How to generate PDF from XHTML or XML source using a Java Applet

Here is an example on how to use a Java Applet to generate a PDF from XHTML or XML source. The main advantage of using an Applet to generate from XHTML or XML source is XHTML/XML source can be gzip compressed from the source web server which reduces very much its size to be transferred over a slow network connection and is speedier, while compared to an actual PDF file that’s transferred over the same network.

The SimplePDFApplet The work involves the development of a SimplePDFApplet which involves the use of library from the BFO Big Faceless report generator and jPDFViewer from Qoppa

The SimplePDFApplet involves the two simple steps below:

1. It uses the Big Faceless report generator to generate the PDF and saves on the local hard drive of the client PC.

2. It then uses the jPDFViewer to open the generated PDF that’s been saved on the local hard drive.

The codes of step 1 and step 2 are coded in a private class LoadPDFRunnable that allows it to run as a runnable thread and a method loadPDF() in the SimplePDFApplet, respectively as follows:

Code of Step 1

    private class LoadPDFRunnable implements Runnable
    {
        private String urlofxml;
 
        private String outputPath;
 
        private SimplePDFApplet a;
 
        public LoadPDFRunnable (SimplePDFApplet a, String outputPath,String urlofxml)
        {
            this.urlofxml = urlofxml;
            this.outputPath = outputPath;
            this.a= a;
        }
 
        public void run()
        {
            try
            {
                a.setPDFLocalFile( genPdf() );
            }
            catch (Exception pdfE)
            {
                displayError (pdfE.getMessage());
            }
 
        }
 
        private String genPdf() throws Exception
		{
		   ReportParser p = ReportParser.getInstance();
		   PDF pdf = p.parse(this.urlofxml );
		   /** The output file stored in c:/mypdfs */
		   String ofile =this.outputPath+"/f"+System.currentTimeMillis()+".pdf";
 
		   pdf.render(new FileOutputStream(ofile));
		   return ofile;
		}
    }

Code of Step 2

  public void loadPDF ()
    {
        try
        {
			/**
            // Load the PDF in the swing thread
            if (SwingUtilities.isEventDispatchThread())
            {
                getPDFViewerBean().loadPDF(this.pdfLFile);
            }
            else
            {
                // Create PDF loader and call it in the Swing event thread
                LoadPDFRunnable loader = new LoadPDFRunnable (this,getParams("outputPath"), getParams("xmlURL") );
                SwingUtilities.invokeAndWait(loader);
            }*/
            LoadPDFRunnable loader = new LoadPDFRunnable (this,getParams("outputPath"), getParams("xmlURL") );
            SwingUtilities.invokeAndWait(loader);
 
            /** Now load from the client's PC hard drive */
            getPDFViewerBean().loadPDF(this.pdfLFile);
            System.out.println("Loading PDF....");
 
 
        }
        catch (Throwable t)
        {
            if (t.getCause() instanceof PDFException)
            {
                displayError (t.getCause().getMessage());
            }
            else
            {
                displayError ("Error loading PDF: " + t.getMessage());
            }
        }
    }

The complete SimplePDFApplet.java

package dunco.applet;
 
import javax.swing.*;
 
import com.qoppa.pdf.PDFException;
import com.qoppa.pdfViewer.PDFViewerBean;
 
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
 
import java.io.*;
import org.xml.sax.*;
import org.faceless.pdf2.*;
import org.faceless.report.*;
 
import java.util.HashMap;
 
 
/**
 * @author Modified from Gerald Holmann's PDFViewerApplet
 *
 */
public class SimplePDFApplet extends JApplet
{
	private PDFViewerBean m_ViewerBean = null;
	private JPanel jPanel = null;
 
	public final static String STRING_FALSE = "False";
	public final static String NUMBER_FALSE = "0";
	public final static String STRING_TRUE = "True";
	public final static String NUMBER_TRUE = "1";
    private JPanel ToolbarOptions = null;
    private JCheckBox jcbShowToolbar = null;
    private JCheckBox jcbShowSelectToolbar = null;
    private String pdfLFile;
 
    private java.util.HashMap params;
 
    private class LoadPDFRunnable implements Runnable
    {
        private String urlofxml;
 
        private String outputPath;
 
        private SimplePDFApplet a;
 
        public LoadPDFRunnable (SimplePDFApplet a, String outputPath,String urlofxml)
        {
            this.urlofxml = urlofxml;
            this.outputPath = outputPath;
            this.a= a;
        }
 
        public void run()
        {
            try
            {
                a.setPDFLocalFile( genPdf() );
            }
            catch (Exception pdfE)
            {
                displayError (pdfE.getMessage());
            }
 
        }
 
        private String genPdf() throws Exception
		{
		   ReportParser p = ReportParser.getInstance();
		   PDF pdf = p.parse(this.urlofxml );
		   /** The output file stored in c:/mypdfs */
		   String ofile =this.outputPath+"/f"+System.currentTimeMillis()+".pdf";
 
		   pdf.render(new FileOutputStream(ofile));
		   return ofile;
		}
    }
 
 
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void init() 
	{
        this.setContentPane(getJPanel());
	}
 
	public void setParams(String n, String v)
	{
		if ( params==null ) params = new HashMap ();
		params.put (n,v);
	}
 
	public String getParams (String n)
	{
		if ( params!=null )
		{
			return (String) params.get (n);
		}
		return getParameter (n);
	}
 
	public void start ()
	{
	    // Set open button visibility according to the BrowseAllowed flag.
	    getPDFViewerBean().getToolbar().getjbOpen().setVisible (toBoolean (getParams("BrowseAllowed")));
 
	    // Set print button visibility according to the PrintAllowed flag.
	    getPDFViewerBean().getToolbar().getjbPrint().setVisible (toBoolean (getParams ("PrintAllowed")));
 
        // Set the toolbar to visible / not visible according to the ToolbarVisible flag
        getPDFViewerBean().getToolbar().setVisible (toBoolean (getParams ("ToolbarVisible")));
 
        int scale = toInteger (getParams ("Scale"));
        if (scale > 0)
        {
            getPDFViewerBean().setScale(scale);
        }
 
        // Set the thumbnail pane visible / not visible according to the ThumbnailsVisible flag
        boolean splitVisible = toBoolean (getParams ("ThumbnailsVisible"));
        if (splitVisible == false)
        {
            getPDFViewerBean().setSplitPolicy(PDFViewerBean.SPLITPOLICY_NEVER_VISIBLE);
        }
 
        // Generate and open the PDF file now
        loadPDF();
	}
 
    private int toInteger (String str)
    {
        try
        {
            return Integer.parseInt(str);
        }
        catch (NumberFormatException nfe)
        {
            return 0;
        }
    }
 
    private boolean toBoolean (String str)
	{
	    if (str == null || str.equalsIgnoreCase(STRING_TRUE) || str.equalsIgnoreCase(NUMBER_TRUE))
	    {
	        return true;
	    }
	    return false;
	}
 
	void setPDFLocalFile (String pdfLFile )
	{
		this.pdfLFile = pdfLFile;	
	}
 
    public void loadPDF ()
    {
        try
        {
			/**
            // Load the PDF in the swing thread
            if (SwingUtilities.isEventDispatchThread())
            {
                getPDFViewerBean().loadPDF(this.pdfLFile);
            }
            else
            {
                // Create PDF loader and call it in the Swing event thread
                LoadPDFRunnable loader = new LoadPDFRunnable (this,getParams("outputPath"), getParams("xmlURL") );
                SwingUtilities.invokeAndWait(loader);
            }*/
            LoadPDFRunnable loader = new LoadPDFRunnable (this,getParams("outputPath"), getParams("xmlURL") );
            SwingUtilities.invokeAndWait(loader);
 
            /** Now load from the client's PC hard drive */
            getPDFViewerBean().loadPDF(this.pdfLFile);
            System.out.println("Loading PDF....");
 
 
        }
        catch (Throwable t)
        {
            if (t.getCause() instanceof PDFException)
            {
                displayError (t.getCause().getMessage());
            }
            else
            {
                displayError ("Error loading PDF: " + t.getMessage());
            }
        }
    }
 
	/**
	 * This method initializes PDFViewerBean	
	 * 	
	 * @return com.qoppa.pdfViewer.PDFViewerBean	
	 */    
	private PDFViewerBean getPDFViewerBean() 
    {
		if (m_ViewerBean == null) {
            m_ViewerBean = new PDFViewerBean();
            m_ViewerBean.setName("PDFViewerBean");
            m_ViewerBean.add(getToolbarOptions(), java.awt.BorderLayout.SOUTH);
		}
		return m_ViewerBean;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout ());
			jPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED));
			jPanel.add(getPDFViewerBean(), BorderLayout.CENTER);
		}
		return jPanel;
	}
    /**
     * This method initializes ToolbarOptions	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getToolbarOptions()
    {
        if (ToolbarOptions == null)
        {
            ToolbarOptions = new JPanel();
            ToolbarOptions.setLayout(new BoxLayout(getToolbarOptions(), BoxLayout.Y_AXIS));
            ToolbarOptions.add(getJcbShowToolbar(), null);
            ToolbarOptions.add(getJcbShowSelectToolbar(), null);
        }
        return ToolbarOptions;
    }
    /**
     * This method initializes jcbShowToolbar	
     * 	
     * @return javax.swing.JCheckBox	
     */
    private JCheckBox getJcbShowToolbar()
    {
        if (jcbShowToolbar == null)
        {
            jcbShowToolbar = new JCheckBox();
            jcbShowToolbar.setText("Show Toolbar");
            jcbShowToolbar.setSelected(true);
            jcbShowToolbar.addChangeListener(new javax.swing.event.ChangeListener() { 
                public void stateChanged(javax.swing.event.ChangeEvent e)
                {
                    getPDFViewerBean().getToolbar().setVisible(jcbShowToolbar.isSelected());
                }
            });
        }
        return jcbShowToolbar;
    }
    /**
     * This method initializes jcbShowSelectToolbar	
     * 	
     * @return javax.swing.JCheckBox	
     */
    private JCheckBox getJcbShowSelectToolbar()
    {
        if (jcbShowSelectToolbar == null)
        {
            jcbShowSelectToolbar = new JCheckBox();
            jcbShowSelectToolbar.setText("Show Select Toolbar");
            jcbShowSelectToolbar.setSelected(true);
            jcbShowSelectToolbar.addChangeListener(new javax.swing.event.ChangeListener() { 
                public void stateChanged(javax.swing.event.ChangeEvent e)
                {
                    getPDFViewerBean().getSelectToolbar().setVisible(jcbShowSelectToolbar.isSelected());
                }
            });
        }
        return jcbShowSelectToolbar;
    }
 
    private void displayError (String errorMsg)
    {
        JOptionPane.showMessageDialog(SimplePDFApplet.this, errorMsg);
    }
 
    /** The main method that allows it to run as a standalone program */    
    public static void main(String[] args) {
 
      // Create an instance of the applet class.
      SimplePDFApplet applet = new SimplePDFApplet();
 
	  applet.setParams("BrowseAllowed", "true" );
	  applet.setParams("PrintAllowed", "true" );
	  applet.setParams("ToolbarVisible", "true" );
	  applet.setParams ("ThumbnailsVisible","true");
	  applet.setParams ("outputPath",args[0]);
	  applet.setParams ("xmlURL",args[1]);
 
      // Send the applet an init() message.
      applet.init();
 
      // Construct a JFrame.
      final JFrame frame =
              new JFrame(applet.getClass().getName ());
 
      // Transfer the applet's context pane to the JFrame.
      frame.setContentPane(applet.getContentPane());
 
      // Transfer the applet's menu bar into the JFrame.
      // This line can be omitted if the applet
      // does not create a menu bar.
      frame.setJMenuBar(applet.getJMenuBar());
 
      // Make the application shut down when the user clicks
      // on the close button.
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
      // Set the size of the frame.
      // To pack the frame as tightly as possible
      // replace the setSize() message with the following.
      // frame.pack();
      frame.setSize(800, 800);
 
      // Set the location of the frame.
      frame.setLocation(100, 100);
 
      // Show the frame.
      frame.setVisible(true);
 
      // Invoke the applet's start() method.
      // This line can be omitted if the applet
      // does not define a start method.
      applet.start();
 
    }
 
 
}  //  @jve:decl-index=0:visual-constraint="10,10"

Sign the Java Applet – The above SimplePDFApplet and the accompanying jar files must be signed as it needs to access the local hard drive of the client PC. The signed jar files are then uploaded to the web server in folder /pdf-applet/ in this example, where the Applet is to be downloaded.

Prepare the HTML file on your web server which outputs the Applet. This could be a JSP or PHP or just plain HTML. The HTML page must contain something similar as follows

<html>
<head><title>PDFViewer</title></head><body>
<object name="PDFViewer" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
		width="100%" height="600" align="baseline" 
		codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-i586.cab#Version=1,4,0,0">
		<param name="code" value="dunco.applet.SimplePDFApplet">
            <param name="archive" value="/pdf-applet/dunco.jar,/pdf-applet/jPDFViewerS.jar,/pdf-applet/bforeport.jar">
            <param name="type" value="application/x-java-applet;jpi-version=1.4.2">
            <param name="scriptable" value="true">
            <param name="progressbar" value="true">
		<param name="xmlUrl" value="http://www.ajaxapp.com/egapp/genpdfxml.php">
		<param name="outputPath" value="c:/mypdfs/">
		<param name="BrowseAllowed" value="true">
		<param name="PrintAllowed" value="true">
		<param name="ToolbarVisible" value="true">
		<param name="ThumbnailsVisible" value="true">
            <embed
			width="100%" height="600" align="baseline" code="jPDFViewerSamples.PDFViewerApplet"
			archive="/pdfviewer/demo/vsamplesS.jar,/pdfviewer/demo/jPDFViewerS.jar"
			type="application/x-java-applet;version=1.4.2"
			xmlUrl="http://www.ajaxapp.com/egapp/genpdfxml.php" outputPath="c:/mypdfs/"
                        scriptable="true" progressbar="true" 
			browseallowed="true"
			printallowed="true"
			toolbarvisible="true"
			thumbnailsvisible="true"
		></embed>
	</object>
</body></html>

The server-side that outputs the XHTML On the server side, the JSP or PHP that outputs the XHTML for the PDF to be generated must follow the format documented by Big Faceless report generator. An example that’s written in PHP with gzip compression enabled and hosted here, is as follows:

<?php 
ob_start("flatcode"); 
echo "<?xml version=\"1.0\"?>";?> 
<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<head>
<meta name="title" value="My First Document"/>
</head>
<body background-color="yellow" font-size="18">
<table style="background:#fff;border:1px solid #ccc;padding:3px;width:800px;">
<?php
 
	$t= 50;
	if ( isset($_REQUEST['t'])) $t=$_REQUEST['t'];
	for ($r=0; $r< $t; $r++)
	{
		 $rr=$r+1;
?>
	<tr>
		<td width="10%"><?php echo $rr;?></td>
		<td width="40%">Item Name <?php echo $rr;?></td>
		<td width="50%">Item Description <?php echo $rr;?></td>
	</tr>
<?php
	}
?>
</table>
</body>
</pdf>
<?php ob_end_flush();
 
/**
The function that flats and compresses the line of code
*/
function flatcode($buffer,$mode)
{
	return ob_gzhandler(str_replace("\r","",str_replace("\n", "",$buffer)),$mode);
	//return ob_gzhandler($buffer,$mode);
}
 
?>

Enter your email address to subscribe our newsletter or feed for FREE:

Delivered by FeedBurner


Bookmark with:

[Delicious]    [Digg]    [Reddit]    [Facebook]    [StumbleUpon]

0 Responses to “How to generate PDF from XHTML or XML source using a Java Applet”


  1. No Comments

Leave a Reply

You must login to post a comment.