xml-xsl-fo-pdf
Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
Obě strany předchozí revizePředchozí verze | Poslední revizeObě strany příští revize | ||
xml-xsl-fo-pdf [2013/08/26 17:20] – [Instalace] blazek | xml-xsl-fo-pdf [2013/09/25 12:15] – [Spuštění] blazek | ||
---|---|---|---|
Řádek 151: | Řádek 151: | ||
shell# ./ | shell# ./ | ||
</ | </ | ||
+ | |||
+ | === JAVA FOP class === | ||
+ | |||
+ | <file java FopGen.java> | ||
+ | package test; | ||
+ | |||
+ | import org.apache.log4j.Logger; | ||
+ | import java.io.File; | ||
+ | import java.io.OutputStream; | ||
+ | import javax.xml.transform.Transformer; | ||
+ | import javax.xml.transform.TransformerFactory; | ||
+ | import javax.xml.transform.Source; | ||
+ | import javax.xml.transform.Result; | ||
+ | import javax.xml.transform.stream.StreamSource; | ||
+ | import javax.xml.transform.sax.SAXResult; | ||
+ | import org.apache.fop.apps.FOUserAgent; | ||
+ | import org.apache.fop.apps.Fop; | ||
+ | import org.apache.fop.apps.FopFactory; | ||
+ | import org.apache.fop.apps.MimeConstants; | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * @author blazek | ||
+ | */ | ||
+ | public class FopGen { | ||
+ | |||
+ | static Logger logger = Logger.getLogger(FopGen.class); | ||
+ | private String xconf; | ||
+ | private String fontBase; | ||
+ | private String xmlInput; | ||
+ | private String xsltInput; | ||
+ | private String pdfOutput; | ||
+ | |||
+ | public FopGen() { | ||
+ | } | ||
+ | |||
+ | public FopGen(String xconf, String fontBase, String xmlInput, String xsltInput, String pdfOutput) { | ||
+ | this.xconf = xconf; | ||
+ | this.fontBase = fontBase; | ||
+ | this.xmlInput = xmlInput; | ||
+ | this.xsltInput = xsltInput; | ||
+ | this.pdfOutput = pdfOutput; | ||
+ | } | ||
+ | |||
+ | public String getXconf() { | ||
+ | return xconf; | ||
+ | } | ||
+ | |||
+ | public void setXconf(String xconf) { | ||
+ | this.xconf = xconf; | ||
+ | } | ||
+ | |||
+ | public String getFontBase() { | ||
+ | return fontBase; | ||
+ | } | ||
+ | |||
+ | public void setFontBase(String fontBase) { | ||
+ | this.fontBase = fontBase; | ||
+ | } | ||
+ | |||
+ | public String getXmlInput() { | ||
+ | return xmlInput; | ||
+ | } | ||
+ | |||
+ | public void setXmlInput(String xmlInput) { | ||
+ | this.xmlInput = xmlInput; | ||
+ | } | ||
+ | |||
+ | public String getXsltInput() { | ||
+ | return xsltInput; | ||
+ | } | ||
+ | |||
+ | public void setXsltInput(String xsltInput) { | ||
+ | this.xsltInput = xsltInput; | ||
+ | } | ||
+ | |||
+ | public String getPdfOutput() { | ||
+ | return pdfOutput; | ||
+ | } | ||
+ | |||
+ | public void setPdfOutput(String pdfOutput) { | ||
+ | this.pdfOutput = pdfOutput; | ||
+ | } | ||
+ | |||
+ | public boolean createPDF() { | ||
+ | |||
+ | try { | ||
+ | // configure fopFactory as desired | ||
+ | FopFactory fopFactory = FopFactory.newInstance(); | ||
+ | |||
+ | // configure fonts | ||
+ | fopFactory.setUserConfig(this.xconf); | ||
+ | fopFactory.getFontManager().setFontBaseURL(" | ||
+ | FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | ||
+ | |||
+ | // Setup output | ||
+ | OutputStream out = new java.io.FileOutputStream(new File(this.pdfOutput)); | ||
+ | out = new java.io.BufferedOutputStream(out); | ||
+ | |||
+ | try { | ||
+ | // Construct fop with desired output format | ||
+ | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, | ||
+ | |||
+ | // Setup XSLT | ||
+ | TransformerFactory factory = TransformerFactory.newInstance(); | ||
+ | Transformer transformer = factory.newTransformer(new StreamSource(new File(this.xsltInput))); | ||
+ | |||
+ | // Setup input for XSLT transformation | ||
+ | Source src = new StreamSource(new File(this.xmlInput)); | ||
+ | |||
+ | // 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, | ||
+ | } finally { | ||
+ | out.close(); | ||
+ | } | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(System.err); | ||
+ | System.exit(-1); | ||
+ | } | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | </ |
xml-xsl-fo-pdf.txt · Poslední úprava: 2023/12/26 19:13 autor: 127.0.0.1