Differenze tra le versioni di "Programmazione:Java/XML to PDF with fop"
Da WikiSitech.
Vai alla navigazioneVai alla ricerca| (6 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
| + | [[Programmazione:Java|<< Back to Java]] | ||
| + | |||
Per ottenere un PDF da un XML è possibile utilizzare la libreria FOP fornita da apache (http://xmlgraphics.apache.org/fop/).<br /><br/> | 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 | + | Per effettuare questa trasformazione abbiamo bisogno fondamentalmente di 3 cose:<br/> |
* Libreria del FOP preso dal sito di apache. | * Libreria del FOP preso dal sito di apache. | ||
* XSL scritto in XSL-FO. | * XSL scritto in XSL-FO. | ||
* XML da trasformare. | * XML da trasformare. | ||
| + | Tutorial sull'XSL-FO si possono trovare su [http://www.w3schools.com/xslfo/default.asp w3schools] e su [http://www.renderx.com/tutorial.html renderX].<br /><br /> | ||
| + | Il procedimento da seguire per la trasformazione è abbastanza semplice, l'esempio è preso da una servlet che ottiene un'XML dentro un DOCUMENT di Dom4J e mette dentro la response il PDF generato dal FOP. | ||
| − | + | <syntaxhighlight lang="java"> | |
| − | |||
//Ottengo l'XML | //Ottengo l'XML | ||
| − | Document xml = <XML>; | + | Document xml = "<XML>"; |
// Creo un'istanza della fopFactory | // Creo un'istanza della fopFactory | ||
| Riga 17: | Riga 20: | ||
// Preparo la response | // Preparo la response | ||
HttpServletResponse resp = fsc.getResp(); | HttpServletResponse resp = fsc.getResp(); | ||
| − | |||
resp.setContentType("application/pdf"); | resp.setContentType("application/pdf"); | ||
resp.setHeader("Expires","-1"); | resp.setHeader("Expires","-1"); | ||
| Riga 23: | Riga 25: | ||
// Ottengo l'XSL scritto in XSL-FO | // Ottengo l'XSL scritto in XSL-FO | ||
| − | InputStream xsltfile = <XSL>; | + | InputStream xsltfile = "<XSL>"; |
| + | |||
| + | // Ottengo lo Stream di output | ||
| + | OutputStream out = resp.getOutputStream(); | ||
// Creo un'istanza del motore FOP | // Creo un'istanza del motore FOP | ||
| Riga 45: | Riga 50: | ||
// Effettuo la trasformazione | // Effettuo la trasformazione | ||
transformer.transform(src, res); | transformer.transform(src, res); | ||
| − | </ | + | </syntaxhighlight> |
Versione attuale delle 09:45, 21 dic 2016
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 3 cose:
- Libreria del FOP preso dal sito di apache.
- XSL scritto in XSL-FO.
- XML da trasformare.
Tutorial sull'XSL-FO si possono trovare su w3schools e su renderX.
Il procedimento da seguire per la trasformazione è abbastanza semplice, l'esempio è preso da una servlet che ottiene un'XML dentro un DOCUMENT di Dom4J e mette dentro la response il PDF generato dal FOP.
//Ottengo l'XML
Document xml = "<XML>";
// Creo un'istanza della fopFactory
FopFactory fopFactory = FopFactory.newInstance();
// Creo un'istanza del foUserAgent
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Preparo la response
HttpServletResponse resp = fsc.getResp();
resp.setContentType("application/pdf");
resp.setHeader("Expires","-1");
resp.setHeader("Cache-Control","no-store");
// Ottengo l'XSL scritto in XSL-FO
InputStream xsltfile = "<XSL>";
// Ottengo lo Stream di output
OutputStream out = resp.getOutputStream();
// Creo un'istanza del motore FOP
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Preparo il transformerFactory
TransformerFactory factory = TransformerFactory.newInstance();
// Creo un'istanza di transformer passandogli l'XSL
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Setto la versione dei parametri
transformer.setParameter("versionParam", "2.0");
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Il transformer ha bisogno di un Source come sorgente quindi ne creo uno
StringReader sr = new StringReader(xml.asXML());
Source src = new StreamSource(sr);
// Effettuo la trasformazione
transformer.transform(src, res);