Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void saveChartAsPDF(JFreeChart chart, File fileName, int width, int height) throws IOException, TransformerException, FOPException {
- File pdfRootDirectory = new File(configRootDirectory.getAbsolutePath()+System.getProperty("file.separator")+"pdf");
- DOMImplementation domImpl= GenericDOMImplementation.getDOMImplementation();
- // Create an instance of org.w3c.dom.Document
- Document document = domImpl.createDocument(null, "svg", null);
- // Create an instance of the SVG Generator
- SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
- // set the precision to avoid a null pointer exception in Batik 1.5
- svgGenerator.getGeneratorContext().setPrecision(6);
- // Ask the chart to render into the SVG Graphics2D implementation
- chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
- // Finally, stream out SVG to a file using UTF-8 character to
- // byte encoding
- boolean useCSS = true;
- Writer w = new OutputStreamWriter(
- new FileOutputStream(new File("test.svg")), "UTF-8");
- svgGenerator.stream(w, useCSS);
- // <editor-fold defaultstate="collapsed" desc="Export chart as PNG file">
- ChartUtilities.saveChartAsPNG(new File(pdfRootDirectory.getAbsolutePath()+ System.getProperty("file.separator") +"tmp.png"), chart, width, height);
- // </editor-fold>
- File xslt = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xslt"+System.getProperty("file.separator")+"stylesheet.xsl");
- File xml = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xml" + System.getProperty("file.separator") + "data.xml");
- File fo = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "fo" + System.getProperty("file.separator") + "temp.fo");
- convertXML2FO(xml, xslt, fo);
- FopFactory fopFactory = FopFactory.newInstance();
- OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
- try {
- Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
- Source src = new StreamSource(fo);
- Result res = new SAXResult(fop.getDefaultHandler());
- transformer.transform(src, res);
- } finally {
- out.close();
- }
- }
- /**
- * Converts an XML file to an XSL-FO file using JAXP (XSLT).
- * @see http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2FO.java?view=markup
- * @param xml the XML file
- * @param xslt the stylesheet file
- * @param fo the target XSL-FO file
- * @throws IOException In case of an I/O problem
- * @throws TransformerException In case of a XSL transformation problem
- */
- public void convertXML2FO(File xml, File xslt, File fo) throws IOException, TransformerException {
- //Setup output
- OutputStream out = new java.io.FileOutputStream(fo);
- try {
- //Setup XSLT
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer(new StreamSource(xslt));
- //Setup input for XSLT transformation
- Source src = new StreamSource(xml);
- //Resulting SAX events (the generated FO) must be piped through to FOP
- Result res = new StreamResult(out);
- //Start XSLT transformation and FOP processing
- transformer.transform(src, res);
- } finally {
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment