Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am using xsl-fo to generate pdf.

Status
Not open for further replies.

ckli

Programmer
Sep 25, 2002
3
HK
I am using xsl-fo to generate pdf. HOwever I encounter
org.xml.sax.SAXException: Can't have more than one root on a DOM!

My code is as below:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();


Element root = doc.createElement("page");

Element reportElem = doc.createElement("work-order");
reportElem.appendChild(doc.createTextNode("123"));
root.appendChild(reportElem);

Element reportElem1 = doc.createElement("company");
reportElem1.appendChild(doc.createTextNode("company"));
root.appendChild(reportElem1);
Element reportElem2 = doc.createElement("project");
reportElem2.appendChild(doc.createTextNode("Project_name"));
root.appendChild(reportElem2);

doc.appendChild(root);

String xsltFile = getServletContext().getRealPath(
getServletContext().getInitParameter("costEstReport1"));
xsltFile = "file:///" + xsltFile.replace('\\', '/');

// Fop Driver initial setup
org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver();
driver.setRenderer(org.apache.fop.apps.Driver .RENDER_PDF);
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
driver.setOutputStream(pdfOut);

// Combine XML, as DOM, with XSL to form FO as DOM
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xsltFile));

DOMResult domResult = new DOMResult();
transformer.transform(new DOMSource(doc), domResult);
driver.render((Document)domResult.getNode());

// Write the output to the file and close
byte[] content = pdfOut.toByteArray();

ServletOutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "inline; filename=\""+request.getParameter("1")+"_"+request.getParameter("1")+".pdf\"");
response.setHeader("Pragma","Public");
response.setContentType("application/pdf");
response.setContentLength(content.length);
os.write(content);
os.flush();
os.close();

Can anybody help me and point out why I get it wrong? Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top