advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs