* @return ByteArrayOutputStream containing the binary representation of a PDF document
* @throws GeneralException
*/
public static ByteArrayOutputStream render(Writer writer) throws GeneralException {
FopFactory fopFactory = ApacheFopWorker.getFactoryInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
Transformer transformer = transFactory.newTransformer();
// set the input source (XSL-FO) and generate the PDF
Reader reader = new StringReader(writer.toString());
Source src = new StreamSource(reader);
// Get handler that is used in the generation process
Result res = new SAXResult(fop.getDefaultHandler());
try {
// Transform the FOP XML source into a PDF, hopefully...
transformer.transform(src, res);
// We don't want to cache the images that get loaded by the FOP engine
fopFactory.getImageFactory().clearCaches();
return out;
} catch (TransformerException e) {
Debug.logError("FOP transform failed:" + e, module );