1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| private ByteArrayOutputStream convertXmlToPdf(Document doc, File xsltFile)
throws IOException, FOPException, TransformerException, ApplicationException, ConfigurationException, SAXException {
// Step 1: Construct a FopFactory
FopFactory fopFactory = FopFactory.newInstance();
// Disable strict validation
fopFactory.setStrictValidation(false);
// Step 2: Set up output stream.
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, pdfOutputStream);
// Step 4: Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xstlSource = new StreamSource(xsltFile);
Transformer transformer = factory.newTransformer(xstlSource);
// concaténation de la liste des icn des illustrations à afficher dans le PDF
// filtrées sur l'applicabilité
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setParameter("imagesPath", clientHelper.getPathManager().getImagesPath());
transformer.setParameter("langPackage", clientHelper.getPackageLanguage());
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
DOMSource xmlSource = new DOMSource(doc);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(xmlSource, res);
return pdfOutputStream;
} |