Here's how I'm calling the FOP process. Prior to this, I have a db call to grab
the stylesheet, and then I pass something like this:
FOPConverter.createPDF(dom, styles, response) and the code below executes. I'm
running the stylesheet from a db call right now, but still pulling the actual
xml file from my local machine (trying to take it 1 step at a time =] ). If you
need more, let me know.
public static void createPDF(Document dom, String xsltStyle,
HttpServletResponse response) {
try {
// Setup directories
File baseDir = new
File("C:/java/projects/rice/kew/src/main/java/edu/iu/uis/eden/edl/");
// Setup input and output files
File xmlfile = new File(baseDir, "projectteam.xml");
//File xsltfile = new File(baseDir, "projectteam2fo.xsl");
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
// Setup output
OutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, out);
// Setup XSLT
TransformerFactory factory =
TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new
DOMSource(getDocumentFromXMLString(xsltStyle)));
//Transformer transformer = factory.newTransformer(new
StreamSource(xsltStyle));
// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "0.9.1.3");
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
//Source src = new DOMSource(dom);
// Resulting SAX events (the generated FO) must be piped
through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
where the 'getDocumentFromXMLString' method simply converts a string to a
document object, if there's a better/easier way let me know.
Heres that method:
public static Document getDocumentFromXMLString(String xmlString) {
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new
StringReader(xmlString)));
return document;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
} catch (SAXException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
From: Griffin,Sean [mailto:[email protected]]
Sent: Tuesday, December 16, 2008 4:26 PM
To: [email protected]
Subject: RE: FOP XML->XSLT->FOP PDF Issues
Greg,
Can you show how you are calling into FOP? Through the command-line or
embedded in your Java app? FOP can take either an XSL-FO document or an
XML/XSLT document pair that generates the XSL-FO and then turns around and
processes that FO. While I'm guessing a bit here, this error seems like
something that would happen if you were using the XSL-FO input example but
passing in an XSLT stylesheet instead of an XSL-FO doc.
For example, to pass XML/XSLT into FOP do something like this (taken from the
FOP examples):
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new
StreamSource(xsltfile));
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
But to just pass in an FO doc:
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Source src = new StreamSource(fo);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
Something else to try is to just take FOP out of the equation and do a simple
XSLT transformation. You would expect an XSL-FO document as the result of the
transformation, and if that doesn't happen then that would point you to why FOP
is choking.
Sean
From: Patterson, Gregory Michael [mailto:[email protected]]
Sent: Tuesday, December 16, 2008 3:16 PM
To: [email protected]
Subject: FOP XML->XSLT->FOP PDF Issues
Hello,
I'm having some trouble getting the FOP to properly use an xslt stylesheet.
I am getting a ValidationException saying I need to put fo:root at the top.
My first element is an xsl element, but the first fo: element is
fo:root. This is exactly the same format as the examples in the fop
examples packages.
In my case, I need to pull the appropriate xsl from a database, and
below is what I'm getting back:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="fo"
version="1.1">
<xsl:output indent="yes" method="xml" omit-xml-declaration="no"
version="1.0"/>
<xsl:param name="versionParam" select="'1.0'"/>
<!-- ========================= -->
<!-- root element: projectteam -->
<!-- ========================= -->
<xsl:template match="projectteam">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
.
.
.
Here is the error:
(Location of error unknown)org.apache.fop.fo.ValidationException: Error: First
element must be the fo:root formatting object. Found (Namespace URI: "", Local
Name: "stylesheet") instead. Please make sure you're producing a valid XSL-FO
document
Finished.
If you'll notice, the first fo element is fo:root, but I do have other things
(required afaik) before it.
Thanks in advance for any help.
-Greg
________________________________
CONFIDENTIALITY NOTICE This message and any included attachments are from
Cerner Corporation and are intended only for the addressee. The information
contained in this message is confidential and may constitute inside or
non-public information under international, federal, or state securities laws.
Unauthorized forwarding, printing, copying, distribution, or use of such
information is strictly prohibited and may be unlawful. If you are not the
addressee, please promptly delete this message and notify the sender of the
delivery error by e-mail or you may call Cerner's corporate offices in Kansas
City, Missouri, U.S.A at (+1) (816)221-1024.