5 import com.apple.cocoa.foundation.NSData;
8 import org.apache.avalon.framework.ExceptionUtil;
9 import org.apache.avalon.framework.logger.Logger;
10 import org.apache.avalon.framework.logger.ConsoleLogger;
13 import org.apache.fop.apps.Driver;
14 import org.apache.fop.apps.FOPException;
15 import org.apache.fop.messaging.MessageHandler;
18 * This class helps calling FOP from the Obj-C side.
19 * Based on "ExampleFO2PDF.java" from the FOP project.
21 public class FOPWrapper {
24 boolean errorOccurred = false;
26 public NSData convert(NSData foData) throws IOException, FOPException {
29 System.setProperty("java.awt.headless", "true");
30 Driver driver = new Driver();
33 Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
34 driver.setLogger(logger);
35 MessageHandler.setScreenLogger(logger);
37 //Setup Renderer (output format)
38 driver.setRenderer(Driver.RENDER_PDF);
41 ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
43 driver.setOutputStream(out);
46 InputStream in = new java.io.ByteArrayInputStream(foData.bytes(0, foData.length()));
48 driver.setInputSource(new InputSource(in));
52 } catch (Exception e) {
66 return new NSData(out.toByteArray());
70 public String getErrorMessage() {
74 public boolean errorOccurred() {
80 public static void main(String[] args) {
82 System.out.println("FOP ExampleFO2PDF\n");
83 System.out.println("Preparing...");
86 File baseDir = new File(".");
87 File outDir = new File(baseDir, "out");
90 //Setup input and output files
91 File fofile = new File(baseDir, "xml/fo/helloworld.fo");
92 File pdffile = new File(outDir, "ResultFO2PDF.pdf");
94 System.out.println("Input: XSL-FO (" + fofile + ")");
95 System.out.println("Output: PDF (" + pdffile + ")");
97 System.out.println("Transforming...");
99 FOPWrapper app = new FOPWrapper();
100 app.convertFO2PDF(fofile, pdffile);
102 System.out.println("Success!");
103 } catch (Exception e) {
104 System.err.println(ExceptionUtil.printStackTrace(e));