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 | import org.apache.fop.apps.FOPException;
|
---|
18 | import org.apache.fop.apps.FopFactory;
|
---|
19 | import org.apache.fop.apps.Fop;
|
---|
20 | import org.apache.fop.apps.MimeConstants;
|
---|
21 |
|
---|
22 | public class foToPdf {
|
---|
23 |
|
---|
24 | public static void main(String args[]) throws IOException, FOPException,
|
---|
25 | TransformerException {
|
---|
26 |
|
---|
27 | createfoFromPdf("test1","test1");
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static void createPdfFromFo(String fileName) throws IOException, FOPException,
|
---|
31 | TransformerException{
|
---|
32 |
|
---|
33 | createfoFromPdf(fileName,fileName);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException,
|
---|
37 | TransformerException{
|
---|
38 | File file = new File(foFileName+".fo");
|
---|
39 | // Step 1: Construct a FopFactory
|
---|
40 | FopFactory fopFactory = FopFactory.newInstance();
|
---|
41 |
|
---|
42 | // Step 2: Set up output stream.
|
---|
43 | OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
|
---|
44 |
|
---|
45 | try {
|
---|
46 | // Step 3: Construct fop with desired output format
|
---|
47 | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
|
---|
48 |
|
---|
49 | // Step 4: Setup JAXP using identity transformer
|
---|
50 | TransformerFactory factory = TransformerFactory.newInstance();
|
---|
51 | Transformer transformer = factory.newTransformer();
|
---|
52 |
|
---|
53 | // Step 5: Setup input and output for XSLT transformation
|
---|
54 | Source src = new StreamSource(file);
|
---|
55 |
|
---|
56 | // Resulting SAX events (the generated FO) must be piped through to FOP
|
---|
57 | Result res = new SAXResult(fop.getDefaultHandler());
|
---|
58 |
|
---|
59 | // Step 6: Start XSLT transformation and FOP processing
|
---|
60 | transformer.transform(src, res);
|
---|
61 | } finally {
|
---|
62 | out.close();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static void createPdfFromFo(File file,String fileName) throws IOException, FOPException,
|
---|
67 | TransformerException{
|
---|
68 |
|
---|
69 | createfoFromPdf(file,"mytest");
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static void createfoFromPdf(File file, String pdfFileName)throws IOException, FOPException,
|
---|
73 | TransformerException{
|
---|
74 | // Step 1: Construct a FopFactory
|
---|
75 | FopFactory fopFactory = FopFactory.newInstance();
|
---|
76 |
|
---|
77 | // Step 2: Set up output stream.
|
---|
78 | OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
|
---|
79 |
|
---|
80 | try {
|
---|
81 | // Step 3: Construct fop with desired output format
|
---|
82 | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
|
---|
83 |
|
---|
84 | // Step 4: Setup JAXP using identity transformer
|
---|
85 | TransformerFactory factory = TransformerFactory.newInstance();
|
---|
86 | Transformer transformer = factory.newTransformer();
|
---|
87 |
|
---|
88 | // Step 5: Setup input and output for XSLT transformation
|
---|
89 | Source src = new StreamSource(file);
|
---|
90 |
|
---|
91 | // Resulting SAX events (the generated FO) must be piped through to FOP
|
---|
92 | Result res = new SAXResult(fop.getDefaultHandler());
|
---|
93 |
|
---|
94 | // Step 6: Start XSLT transformation and FOP processing
|
---|
95 | transformer.transform(src, res);
|
---|
96 | } finally {
|
---|
97 | out.close();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|