Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange FOP behaviour

 
Philip Hung
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Your sugggestion worked perfectly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic