Differenze tra le versioni di "Programmazione:Java/XML to PDF with fop"
Riga 1: | Riga 1: | ||
− | + | Per ottenere un PDF da un XML è possibile utilizzare la libreria FOP fornita da apache (http://xmlgraphics.apache.org/fop/).<br /><br/> | |
− | + | Per effettuare questa trasformazione abbiamo bisogno fondamentalmente di x cose:<br/> | |
+ | * Libreria del FOP preso dal sito di apache. | ||
+ | * XSL scritto in XSL-FO. | ||
+ | * XML da trasformare. | ||
− | + | <code java> | |
+ | Document xml = self.getXML(utente, uc, fsc); | ||
+ | // configure fopFactory as desired | ||
+ | FopFactory fopFactory = FopFactory.newInstance(); | ||
+ | |||
+ | FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | ||
+ | // configure foUserAgent as desired | ||
+ | |||
+ | // Setup output | ||
+ | HttpServletResponse resp = fsc.getResp(); | ||
+ | OutputStream out = resp.getOutputStream(); | ||
+ | resp.setContentType("application/pdf"); | ||
+ | resp.setHeader("Expires","-1"); | ||
+ | resp.setHeader("Cache-Control","no-store"); | ||
+ | InputStream xsltfile = this.getClass().getResourceAsStream(self.getXSL(utente, uc, fsc)); | ||
+ | if (log.isDebugEnabled()){ | ||
+ | log.debug("XSL per la stampa: "+self.getXSL(utente, uc, fsc)); | ||
+ | } | ||
+ | |||
+ | // Construct fop with desired output format | ||
+ | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | ||
+ | |||
+ | // Setup XSLT | ||
+ | TransformerFactory factory = TransformerFactory.newInstance(); | ||
+ | Transformer transformer = factory.newTransformer(new StreamSource(xsltfile)); | ||
+ | |||
+ | // Set the value of a <param> in the stylesheet | ||
+ | transformer.setParameter("versionParam", "2.0"); | ||
+ | StringReader sr = new StringReader(xml.asXML()); | ||
+ | if (log.isDebugEnabled()){ | ||
+ | log.debug("XML per la stampa: "+D4JXmlTool.xml2String(xml, true)); | ||
+ | } | ||
+ | // Setup input for XSLT transformation | ||
+ | Source src = new StreamSource(sr); | ||
+ | |||
+ | // Resulting SAX events (the generated FO) must be piped through to FOP | ||
+ | Result res = new SAXResult(fop.getDefaultHandler()); | ||
+ | |||
+ | // Start XSLT transformation and FOP processing | ||
+ | transformer.transform(src, res); | ||
+ | </code> |
Versione delle 12:14, 4 gen 2007
Per ottenere un PDF da un XML è possibile utilizzare la libreria FOP fornita da apache (http://xmlgraphics.apache.org/fop/).
Per effettuare questa trasformazione abbiamo bisogno fondamentalmente di x cose:
- Libreria del FOP preso dal sito di apache.
- XSL scritto in XSL-FO.
- XML da trasformare.
Document xml = self.getXML(utente, uc, fsc);
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
// Setup output
HttpServletResponse resp = fsc.getResp();
OutputStream out = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Expires","-1");
resp.setHeader("Cache-Control","no-store");
InputStream xsltfile = this.getClass().getResourceAsStream(self.getXSL(utente, uc, fsc));
if (log.isDebugEnabled()){
log.debug("XSL per la stampa: "+self.getXSL(utente, uc, fsc));
}
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "2.0");
StringReader sr = new StringReader(xml.asXML());
if (log.isDebugEnabled()){
log.debug("XML per la stampa: "+D4JXmlTool.xml2String(xml, true));
}
// Setup input for XSLT transformation
Source src = new StreamSource(sr);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);