public void renderPDF(Document xmlDoc, HttpServletResponse response)
throws ServletException
{try
{
// Lets get the XSL file in stream form
// First get the Serlet Context so a true url can be
// determined
ServletContext sc = this.getServletContext();
URL xslURL = sc.getResource(xslReportDirectory + xslFilename);
Source xslSource = new StreamSource(new java.net.URL(xslURL.toString()).openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
// We're returning a pdf so set it as the content type
response.setContentType("application/pdf");//String output = null;
// Create a Document by creating the DocumentBuilderFactory
// and the Document Builder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document docResult = db.newDocument();
// Create a DOMResult object for the result of the
// transformation, and set its node to be the result Document
DOMResult drResult = new DOMResult(docResult);
Document foOutDoc = null;
// Create the Transformer that takes the XML DOM and converts it
// into XML in memory
TransformerFactory transfact = TransformerFactory.newInstance();
Transformer transformer = transfact.newTransformer(xslSource);
// We need a DOMSource to pass into the transformer
DOMSource dsSource = new DOMSource(xmlDoc);// if the transformer failed in it's creation don't create the new
// FO Document
if (transformer != null)
{
transformer.transform(dsSource, drResult);
foOutDoc = (Document) drResult.getNode();
//System.err.println("output: " + output);
} else
{
System.err.println("No Transformer");
}
//Serialize DOM So we can print it out if we need to
OutputFormat format = new OutputFormat(foOutDoc); // Now convert the Serialized DOM to a XMLSerializer object
// which has a toString() method
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.serialize(foOutDoc); // debug statement to print out the FOXSL file
//System.err.println("foOutDoc: ");
//System.err.println(stringOut.toString()); // if the transformer didn't create fo Document
// throw and exception
if (foOutDoc == null)
{
throw new ServletException("foOutDoc is null");
}// Create the FOP Driver and set the Logger(required), then
// set the Renderer to the output we want which is a pdf
// then set the Output stream that is the ByteArrayOutputStream
// we setup earlier in this method
Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
// This is where the FOP creates the PDF
driver.render(foOutDoc); // Now convert it to a byte array and
// set it as the output stream to the servlet
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception ex)
{
throw new ServletException(ex);
}
}On Feb 8, 2005, at 5:19 PM, Frank Schaare wrote:
Hi,
i�m looking for an Servlet (or struts action) example, the builds the pdf DIRECTLY from the response stream.
I studied the servlet example and googled some time, but all examples deal with files as input.
Has anyone ever realised this task or knows such an example ?
thx...
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
