File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Strange FOP behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Strange FOP behaviour" Watch "Strange FOP behaviour" New topic
Author

Strange FOP behaviour

Philip Hung
Greenhorn

Joined: Dec 11, 2001
Posts: 10
Hi! Can someone please tell me what I'm doing wrong?
I have an XML file and an XSL file (to convert the XML to FO). I executed the commandline version of FOP (fop.bat) as follows:
fop -xml myXML.xml -xsl myXSL.xsl -pdf myPDF.pdf
This works perfectly and the PDF was generated properly. However, when I embed the following code (example from FOP webpage) in my program I'm getting a NullPointerException:
Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
XMLReader parser = inputHandler.getParser();
driver.setOutputStream(new FileOutputStream(outFile));
driver.render(parser, InputHandler.getInputSource());
In addition, I used Xalan to manually convert the XML file to FO and use the other example from the FOP webpage:
Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
driver.setInputSource(new FileInputSource(args[0]));
driver.setOutputStream(new FileOutputStream(args[1]));
driver.run();
This code works perfectly and the PDF was rendered properly. According to FOP FAQ, there must be something wrong in my XSL file or XML file thus it is causing the NullPointerException. However, if this was the case then all attempts should have failed or am I missing something here? Any enlightenment would be greatly appreciated. Thanks.
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
If your above code is not working you can try the below code..

This is working fine for me provided i have vaild xsl and xml.


Spritle Software Blogs
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Philip Hung:
Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
XMLReader parser = inputHandler.getParser();
driver.setOutputStream(new FileOutputStream(outFile));
driver.render(parser, InputHandler.getInputSource());

Or else you can set the driver.setOutputStream to
something like
ByteArrayOutputStream out = new ByteArrayOutputStream();
driver.setOutputStream(out);
and convert this out to bytearray and write to a file.
Philip Hung
Greenhorn

Joined: Dec 11, 2001
Posts: 10
Thanks. Your sugggestion worked perfectly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange FOP behaviour
 
Similar Threads
Please help: Transformer Exception while applying transformation.
Error at runtime
PDF USING JSF & FOP
Question on correct use of I/O stream objects.
Using FOP