source: default/v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/generator/foToPdf.java @ 72

Last change on this file since 72 was 72, checked in by hkaulbersch, 10 years ago

Automatic PDF Generation from .fo files now works

  • Property svn:mime-type set to text/plain
File size: 3.1 KB
Line 
1package de.ugoe.cs.swe.bnftools.generator;
2
3import java.io.BufferedOutputStream;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStream;
8
9import javax.xml.transform.Result;
10import javax.xml.transform.Source;
11import javax.xml.transform.Transformer;
12import javax.xml.transform.TransformerException;
13import javax.xml.transform.TransformerFactory;
14import javax.xml.transform.sax.SAXResult;
15import javax.xml.transform.stream.StreamSource;
16
17
18
19import org.apache.fop.apps.FOPException;
20import org.apache.fop.apps.Fop;
21import org.apache.fop.apps.FopFactory;
22import org.apache.fop.apps.MimeConstants;
23
24
25public class foToPdf {
26
27
28       
29        public static void createPdfFromFo(String fileName) throws IOException, FOPException,
30        TransformerException{
31
32                createfoFromPdf(fileName,fileName);
33        }
34       
35        private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException,
36        TransformerException{
37                File file = new File(foFileName+".fo");
38                // Step 1: Construct a FopFactory
39                FopFactory fopFactory = FopFactory.newInstance();
40
41                // Step 2: Set up output stream.
42                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
43
44                try {
45                        // Step 3: Construct fop with desired output format
46                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
47
48                        // Step 4: Setup JAXP using identity transformer
49                        TransformerFactory factory = TransformerFactory.newInstance();
50                        Transformer transformer = factory.newTransformer();
51
52                        // Step 5: Setup input and output for XSLT transformation
53                        Source src = new StreamSource(file);
54
55                        // Resulting SAX events (the generated FO) must be piped through to FOP         
56                        Result res = new SAXResult(fop.getDefaultHandler());
57
58                        // Step 6: Start XSLT transformation and FOP processing
59                        transformer.transform(src, res);
60                } finally {
61                        out.close();
62                }
63        }
64       
65        public static void createPdfFromFo(File file,String fileName) throws IOException, FOPException,
66        TransformerException{
67
68                createfoFromPdf(file,"mytest");
69        }
70       
71        private static void createfoFromPdf(File file, String pdfFileName)throws IOException, FOPException,
72        TransformerException{
73                // Step 1: Construct a FopFactory
74                FopFactory fopFactory = FopFactory.newInstance();
75
76                // Step 2: Set up output stream.
77                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
78
79                try {
80                        // Step 3: Construct fop with desired output format
81                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
82
83                        // Step 4: Setup JAXP using identity transformer
84                        TransformerFactory factory = TransformerFactory.newInstance();
85                        Transformer transformer = factory.newTransformer();
86
87                        // Step 5: Setup input and output for XSLT transformation
88                        Source src = new StreamSource(file);
89
90                        // Resulting SAX events (the generated FO) must be piped through to FOP         
91                        Result res = new SAXResult(fop.getDefaultHandler());
92
93                        // Step 6: Start XSLT transformation and FOP processing
94                        transformer.transform(src, res);
95                } finally {
96                        out.close();
97                }
98        }
99}
Note: See TracBrowser for help on using the repository browser.