Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
The use of intranets has enabled employees operating in offices spread across the globe to access a centralized application at their company headquarters. The ability to create and print documents over their company's intranet would prove to be valuable functionality for such employees.
With such functionality in place, what would happen if a user (say in Japan or India) wants to print hundreds of PDF documents using a default printer from a centralized application that runs on servers in the U.S. Opening each PDF with Acrobat Reader and printing all the documents individually would not only be time consuming, but also tedious. A solution that fires multiple PDFs to the default printer so the user doesn't need to open Acrobat Reader would prove most helpful.
Formatting Objects Processor (FOP) is an open source Java API that can convert your XML data directly into reports in PDF format. The software is developed under the Apache XML Graphics Project and is free to use. This article shows you how to convert raw data (from Oracle Database) to XML, which, in turn, is converted to reports in PDF format. It is assumed the user has a basic understanding of technologies like Java, XML, BEA WebLogic Server, Enterprise JavaBeans (EJB), FOP, Extensible Stylesheet Language Transformations (XSLT), and Oracle Database.
The figure below gives an overview of the flow of data. The XML data along with the XSL file forms the input to the FOP processor,
which in turn is configured using the userconfig.xml for Unicode fonts.

Overview of Unicode XML to PDF document
Let's assume that the application is an n-tier application, uses a thin client (written with JavaServer Pages (JSP) and Struts), WebLogic, and Oracle Database. The data retrieved from the database is massaged by the beans on WebLogic by EJB components and servlets, and thrown to the client via JSP pages and Struts.
In our case, we need to process a request from the client and send data back to the client as a PDF file, which, in turn, is queued to the local default printer. In an intranet setup like ours, it's best to use the HTTP protocol and set up communication with a servlet to do the necessary work. A servlet, in general, can return any type of data, not just an HTML page. Since we have a thin client, we must use an applet on the client that has code within it to call this servlet.
Let's give the names PrintApplet and PrintServlet to our applet and servlet, respectively. The applet needs to be signed so that it's trusted and has no problems when users
within the intranet access the page containing it. Also, we know that this applet will make HTTP calls to the PrintServlet on the application server.
Let's see what code needs to go within the applet. The flow of the PrintApplet is shown below. Let's assume the user has a search result set displayed on the browser page and wants to print a PDF of one
of the records. The user selects a record and clicks the Print button.
PrintApplet
PrintApplet.java code:
/**
* Import all FOP classes. Check http://xml.apache.org/fop/ for details.
*/
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.apps.TraxInputHandler;
import org.apache.fop.messaging.MessageHandler;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.ConsoleLogger;
public class PrintApplet extends JApplet {
org.apache.avalon.framework.logger.Logger log = null;
private PrinterJob pj = null;
//Initialize the applet.
public void init() {
pj = PrinterJob.getPrinterJob();
pj.setCopies(1);
Locale local = this.getLocale();
}
public void paint(Graphics g) {
}
//Get Applet information.
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info.
public String[][] getParameterInfo() {
return null;
}
public void callFromHTML(Object argument1)
{
try {
URL url = new URL("http://myserver:7011/web/PDFServlet?" +"QString1" + "=" +
URLEncoder.encode(argument1"));
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
PrintWriter toServlet = new PrintWriter(conn.getOutputStream());
toServlet.flush();
toServlet.close();
/**
* In the following 2 code lines, FOP is set to use userconfig.xml file for its configuration.
*
*/
URL userConfig = new URL("http","myserver","7011","/web/userconfig.xml");
org.apache.fop.apps.Options options = new org.apache.fop.apps.Options(userConfig.openStream());
org.apache.fop.apps.XSLTInputHandler input =
new org.apache.fop.apps.XSLTInputHandler(
"http://myserver:7011/web/myXML.xml","http://myserver:7011/web/myXSL.xsl" );
PrintRenderer renderer = new PrintRenderer(pj);
Driver driver = new Driver();
driver.setLogger (log);
driver.setRenderer (renderer);
driver.render(input.getParser(), input.getInputSource());
}
catch (Exception e) {
e.printStackTrace();
}
}
this.getAppletContext().showStatus (" Documents have been printed.");
}
/**
* The PrintRenderer class is obtained from FopPrintServlet.java
* example in FOP jar file. Get the complete class PrintRenderer here.
*
**/ class PrintRenderer extends AWTRenderer {
.
.
.
PrintRenderer(PrinterJob printerJob) {
super(null);
this.printerJob = printerJob;
startNumber = 0 ;
endNumber = -1;
printerJob.setPageable(this);
mode = EVEN_AND_ALL;
String str = System.getProperty("even");
if (str != null) {
try {
mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD;
} catch (Exception e) {}
}
}
.
.
.
.
} // Class PrintRenderer.
}
The following steps briefly explain the creation of PDF files on the server and then how to print them on the client-side printer.
Archived Discussions (Read only)