Tutorials : Using FOP with Java - Part 2 :

The main thing to notice in this example is that we are setting our custom PrintRenderer object to the driver as opposed to the default PDF/PS/PCL renderers available in FOP. This way we ensure that we handle the printing of FO document in our own way. Also, we are using java.awt.print.PrinterJob class to control the printing in the default printer installed in your system.

This example is fairly straightforward but will not work in a UNIX environment as we are using the AWT based print renderer solution. In the next solution, I will show how we can print our FOP formatted document directly to the printer in UNIX environment.

Printing Solution 2

In the previous example, we saw how the intermediate FO document can be used for printing. In this example, we will take a step forward and see how we can directly pipe the formatted FOP output to the printer. In this solution, we are going to render the given XML as a Postscript file and pass it to a printer which is capable of printing Postscript documents.

public void print(Document xmlFile, String xslFile) throws IOException {


String printQName =”testQueue”;

OutputStream out = null;


try {

//load the XSL file as an input Stream

InputStream xslInputStream = this.getClass().getResourceAsStream("/"+xslFile);

//Setup XSLT

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer(new StreamSource(xslInputStream));


//output the XML to the output Stream

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

XMLOutputter outputter = new XMLOutputter();

//outputter.setTextNormalize(false);

outputter.setTextTrim(false);

outputter.output(xmlFile, outStream);

outStream.close();

//create an input Stream to read the JDOM version of the XSML

ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

//Setup input for XSLT transformation

Source src = new StreamSource(inStream);


//create a new OutputStream object to hold the transformed XML

ByteArrayOutputStream formattedStream = new ByteArrayOutputStream();

//Start XSLT transformation and FOP processing

transformer.transform(src, new StreamResult(formattedStream));

//now the XSLT is done, read the transofrmed data to an input Stream

ByteArrayInputStream input = new ByteArrayInputStream(formattedStream.toByteArray());

formattedStream.close();

//create an input source for the FOP driver to use

InputSource inputSource = new InputSource(input);

//now create a UNIX print process and obtain the output stream for the process

//to pipe in the data to it.

Process process = Runtime.getRuntime().exec("lp -d "+printQName);

out = process.getOutputStream();

//create the FOP driver with the input source and set the output stream as the output stream

//from the print process.

Driver driver = new Driver(inputSource, out);

//set the renderer to produce an PostScript output.

driver.setRenderer(Driver.RENDER_PS);

driver.run();

driver.reset();

out.flush();

out.close();

process.waitFor();

} catch (Exception fope) {

fope.printStackTrace();

}

}



Listing 4, A UNIX print queue based solution

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.