Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML TO PDF printing

843834Sep 15 2003 — edited Jul 5 2004
Hi,

I managed to create a pdf File, from a XML document, using XSLT and XSL-FO.

I'd like now to print this pdf File from my Java application. Is there a simple way of doing this ?

Thank you in advance for any help.



------------------------------------------------------------------------
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;
import org.apache.avalon.framework.*;
import org.apache.avalon.framework.logger.*;
import org.apache.fop.apps.*;

/**
* This class converts an XML file to PDF using JAXP (XSLT) and FOP (XSL-FO).
* @author
*/
public class XmlToPdf {

private static final String BASE_DIR = "./test/";
private static final String FILE = "test";

/**
* Converts an XML file to a PDF file using JAXP and FOP.
* @param xmlFile The XML file
* @param xslFile The XSLT stylesheet file
* @param pdfFile The output PDF file
* @throws IOException In case of an I/O problem
* @throws FOPException In case of a FOP problem
* @throws TransformerException In case of a XSL transformation problem
*/
public void convertXML2PDF(File xmlFile, File xsltFile, File pdfFile)
throws IOException, FOPException, TransformerException {
//Construct driver
Driver driver = new Driver();

//Setup logger
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);

//Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);

//Setup output
OutputStream out = new FileOutputStream(pdfFile);
out = new BufferedOutputStream(out);

try {
driver.setOutputStream(out);

//Setup XSLT
TransformerFactory myFactory = TransformerFactory.newInstance();
Transformer myTransformer = myFactory.newTransformer(new StreamSource(xsltFile));

//Setup input for XSLT transformation
Source src = new StreamSource(xmlFile);

//The generated FO (resulting SAX events) must be sent to FOP
Result res = new SAXResult(driver.getContentHandler());

//Start XSLT transformation and FOP processing
myTransformer.transform(src, res);
} finally {
out.flush();
out.close();
}
}

/**
* Main method.
* @param args command-line arguments
*/
public static void main(String[] args) {
try {
System.out.println("Starting XML TO PDF Transformation...\n");

//Base Directory...
File baseDir = new File(BASE_DIR);

//Input and output files...
File xmlFile = new File(baseDir, FILE+".xml");
File xsltFile = new File(baseDir, FILE+".xsl");
File pdfFile = new File(baseDir, FILE+".pdf");

System.out.println("Input: XML (" + xmlFile + ")");
System.out.println("XSL Stylesheet: " + xsltFile);
System.out.println("Output: PDF (" + pdfFile + ")");
System.out.println();
System.out.println("Transforming...");

XmlToPdf app = new XmlToPdf();
// Do the transformation...
app.convertXML2PDF(xmlFile, xsltFile, pdfFile);

System.out.println();
System.out.println("The pdf was created succesfully !\n");
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(-1);
}
}
}



Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 2 2004
Added on Sep 15 2003
3 comments
653 views