View difference between Paste ID: SV37iLU0 and VNpe2vfT
SHOW: | | - or go back to the newest paste.
1
    private void saveChartAsPDF(JFreeChart chart, File fileName, int width, int height) throws IOException, TransformerException, FOPException {
2
        File pdfRootDirectory = new File(configRootDirectory.getAbsolutePath()+System.getProperty("file.separator")+"pdf");
3
4
        DOMImplementation domImpl= GenericDOMImplementation.getDOMImplementation();
5
        // Create an instance of org.w3c.dom.Document
6
        Document document = domImpl.createDocument(null, "svg", null);
7
        // Create an instance of the SVG Generator
8
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
9
        // set the precision to avoid a null pointer exception in Batik 1.5
10
        svgGenerator.getGeneratorContext().setPrecision(6);
11
        // Ask the chart to render into the SVG Graphics2D implementation
12
        chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
13
        // Finally, stream out SVG to a file using UTF-8 character to
14
        // byte encoding
15
        boolean useCSS = true;
16
        Writer w = new OutputStreamWriter(
17
        new FileOutputStream(new File("test.svg")), "UTF-8");
18
        svgGenerator.stream(w, useCSS);
19
20
        // <editor-fold defaultstate="collapsed" desc="Export chart as PNG file">
21
        ChartUtilities.saveChartAsPNG(new File(pdfRootDirectory.getAbsolutePath()+ System.getProperty("file.separator") +"tmp.png"), chart, width, height);
22
        // </editor-fold>
23
24
        File xslt = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xslt"+System.getProperty("file.separator")+"stylesheet.xsl");
25
        File xml = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "xml" + System.getProperty("file.separator") + "data.xml");
26
        File fo = new File(pdfRootDirectory.getAbsolutePath() + System.getProperty("file.separator") + "fo" + System.getProperty("file.separator") + "temp.fo");
27
28
        convertXML2FO(xml, xslt, fo);
29
30
        FopFactory fopFactory = FopFactory.newInstance();
31
32
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
33
        try {
34
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
35
            
36
            TransformerFactory factory = TransformerFactory.newInstance();
37
            Transformer transformer = factory.newTransformer();
38
39
            Source src = new StreamSource(fo);
40
            
41
            Result res = new SAXResult(fop.getDefaultHandler());
42
            
43
            transformer.transform(src, res);
44
        } finally {
45
            out.close();
46
        }
47
    }
48
49
50
    /**
51
    * Converts an XML file to an XSL-FO file using JAXP (XSLT).
52
    * @see http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2FO.java?view=markup
53
    * @param xml the XML file
54
    * @param xslt the stylesheet file
55
    * @param fo the target XSL-FO file
56
    * @throws IOException In case of an I/O problem
57
    * @throws TransformerException In case of a XSL transformation problem
58
    */
59
    public void convertXML2FO(File xml, File xslt, File fo) throws IOException, TransformerException {
60
        //Setup output
61
        OutputStream out = new java.io.FileOutputStream(fo);
62
        try {
63
            //Setup XSLT
64
            TransformerFactory factory = TransformerFactory.newInstance();
65
            Transformer transformer = factory.newTransformer(new StreamSource(xslt));
66
67
            //Setup input for XSLT transformation
68
            Source src = new StreamSource(xml);
69
70
            //Resulting SAX events (the generated FO) must be piped through to FOP
71
            Result res = new StreamResult(out);
72
73
            //Start XSLT transformation and FOP processing
74
            transformer.transform(src, res);
75
        } finally {
76
            out.close();
77
        }
78
    }