Subject: Re: Embedded FOP question.
From: Davor Cengija <[EMAIL PROTECTED]>
===
[EMAIL PROTECTED] wrote:
> I've been using the FopServlet as the basis for my servlet app which takes
> a DOM document, stores it to an xml file, then uses XSLTInputHandler to
> convert to fo and output the pdf. Now I'm getting to the point where I
> want to eliminate the xml file and convert using the DOM object or a
> serialized
> string of the xml. It looks to me like it can't be done with the
> XSLTInputHandler class because it requires java.io.File parameters. I'm
> looking at XSLTransform to transform the DOM document input to a DOM
> document containing FO output using the xsl file. Just wondering if this
> is the best approach and then how to best use the FO DOM document to
> output the
> pdf. If anyone knows where I can find some good sample code (or if you
> have a sample yourself), I would appreciate the tips.
Here is how I convert DOM to PDF directly, without creating files or string
buffers. Methods are pulled out from different classes and not very pretty
formatted but I hope you'll manage it:
// creates FO DOM from node with transformer
protected Document createFo(Node node, Transformer transformer)
throws TransformerConfigurationException,
TransformerException {
Document doc = null;
try {
// helper to create DOM documents
doc = XMLUtilities.createDocument();
} catch (Exception e) {
throw new TransformerConfigurationException();
}
Node n = doc.importNode(node, true);
doc.appendChild(n);
DOMSource domSource = new DOMSource(doc);
DOMResult domResult = new DOMResult();
transformer.transform(domSource, domResult);
Node n2 = domResult.getNode();
return (Document) n2;
}
// transforms FO DOM into PDF and sends it to OutputStream
protected void createPdf(Document doc, OutputStream fOut)
throws IOException, FOPException {
InputSource inputSource = XmlHelper.dom2inputSource(doc);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
Logger log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.ERROR);
File userConfigFile = new File(FOP_CONFIG);
Options options = new Options(userConfigFile);
Driver driver = new Driver(inputSource, out);
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
byte[] content = out.toByteArray();
fOut.write(content);
out.flush();
out.close();
}
/*
import org.apache.xalan.serialize.Serializer;
import org.apache.xalan.serialize.SerializerFactory;
import org.apache.xalan.templates.OutputProperties;
*/
// and dom2inputSource, which converts any DOM to SAX InputSource
without
// writing to disk
public static InputSource dom2inputSource(Document doc)
throws IOException {
InputSource is = null;
Serializer serializer = SerializerFactory.getSerializer(
OutputProperties.getDefaultMethodProperties("xml"));
CharArrayWriter caw = new CharArrayWriter();
serializer.setWriter(caw);
serializer.asDOMSerializer().serialize(doc);
is = new InputSource(new
java.io.CharArrayReader(caw.toCharArray()));
return is;
}
--
Davor Cengija
[EMAIL PROTECTED]
=============================
"Nicht in die Augen bringen!"