Hallo zusammen,
mal wieder ein Problem. Ich will mittels Apache FOP eine XML-Datei (inkl. XSLT-Stylesheet) mittels Java-Code in ein PDF umwandeln. Die Beispiele auf der Apache-Seite hab ich durchgearbeitet und weitestgehend auch verstanden. Allerdings klappt die Umsetzung in meinem Programm nicht so, wie sie soll.
Erstmal mein Code:
File xmlfile = new File("D:/src.xml");
File xsltfile = new File("D:/stylesheet.xsl");
File fofile = new File("D:/fo_objekt.fo");
File pdffile = new File("D:/output.pdf");
//XML2FO
OutputStream out = new FileOutputStream(fofile);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
Source src = new StreamSource(xmlfile);
Result res = new StreamResult(out);
transformer.transform(src, res);
//FO2PDF
FopFactory fopfactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopfactory.newFOUserAgent();
out = new BufferedOutputStream(new FileOutputStream(pdffile));
Fop fop = fopfactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
factory = TransformerFactory.newInstance();
transformer = factory.newTransformer();
src = new StreamSource(fofile);
res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
Soweit so gut. Wenn ich diese Implementierung ausführe, bekomme ich folgende Exception:
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: Error: First element must be the fo:root formatting object. Found (Namespace URI: "", Local Name: "HTML") instead. Please make sure you're producing a valid XSL-FO document.
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:501)
at de.gzs.fop.start.main(start.java:138)
Caused by: org.apache.fop.fo.ValidationException: Error: First element must be the fo:root formatting object. Found (Namespace URI: "", Local Name: "HTML") instead. Please make sure you're producing a valid XSL-FO document.
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:262)
at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:163)
at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1072)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
... 1 more
---------
org.apache.fop.fo.ValidationException: Error: First element must be the fo:root formatting object. Found (Namespace URI: "", Local Name: "HTML") instead. Please make sure you're producing a valid XSL-FO document.
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:262)
at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:163)
at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1072)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
at de.gzs.fop.start.main(start.java:138)
Wenn ich mir die erstellte FO-Datei ansehe, erkenne ich auch das Problem:
Der Output ist kein FO-Objekt, sondern reines HTML.
Weiß jemand, was ich falsch mache?
JDK 1.6
FOP 0.95
Gruß
Die Lady