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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
public static boolean generatePDF(HttpServletResponse response, Object dataToSerialize, XStream xstream, String xslFilePath,String xmlFilePath) {
logger.info("begin generatePDF");
//TODO delete later :DEBUG : affiche dans System.out le résultat de la sérialisation
xstream.toXML(dataToSerialize, System.out);
boolean success = true;
try {
File xmlfile = new File(xmlFilePath);
//File xsltfile = new File(xslFilePath);
FileOutputStream fos = new FileOutputStream(xmlfile);
// DEBUG : affiche dans System.out le résultat de la sérialisation
xstream.toXML(dataToSerialize,fos);
logger.info("DEBUG 1111111111111");
logger.info("DEBUG 22222222222222");
// read xsl file into xslInputStream
File xslFile = new File(xslFilePath);
InputStream xslInputStream = new FileInputStream(xslFile);
//TODO to test to reslove caractères problem
//InputStream xslInputStream = new ByteArrayInputStream(xslFile.toString().getBytes("UTF-8"));
// create output stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
logger.info("DEBUG 444444444444");
// create pdf driver
Driver driver = new Driver();
driver.setLogger(new ConsoleLogger(ConsoleLogger.LEVEL_INFO));
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(outputStream);
logger.info("DEBUG 5555555555555");
// encapsulate objects...
//Source xml = new StreamSource(xmlInputStream);
Source xsl = new StreamSource(xslInputStream);
// Result res = new SAXResult(driver.getContentHandler());
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(driver.getContentHandler());
try {
logger.info("DEBUG 6666666666666");
// transform xml with xsl
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.setParameter("versionParam", "2.0");
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
logger.info("DEBUG 6.1");
// set response with transformed data
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=contrats.pdf");
logger.info("DEBUG 6.2");
response.setContentLength(outputStream.size());
response.getOutputStream().write(outputStream.toByteArray());
logger.info("DEBUG 6.3");
response.getOutputStream().flush();
response.getOutputStream().close();
}
finally {
logger.info("DEBUG 77777777777");
//xmlInputStream.close();
xslInputStream.close();
outputStream.close();
}
}
catch (Exception e) {
success = false;
logger.severe(e.toString());
}
logger.info("end generatePDF");
return success;
} |
Partager