1 | package de.ugoe.cs.swe.bnftools.generator;
|
---|
2 |
|
---|
3 | import java.io.BufferedOutputStream;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.OutputStream;
|
---|
8 |
|
---|
9 | import javax.xml.transform.Result;
|
---|
10 | import javax.xml.transform.Source;
|
---|
11 | import javax.xml.transform.Transformer;
|
---|
12 | import javax.xml.transform.TransformerException;
|
---|
13 | import javax.xml.transform.TransformerFactory;
|
---|
14 | import javax.xml.transform.sax.SAXResult;
|
---|
15 | import javax.xml.transform.stream.StreamSource;
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | import org.apache.fop.apps.FOPException;
|
---|
20 | import org.apache.fop.apps.Fop;
|
---|
21 | import org.apache.fop.apps.FopFactory;
|
---|
22 | import org.apache.fop.apps.MimeConstants;
|
---|
23 |
|
---|
24 |
|
---|
25 | public 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 | }
|
---|