aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.editorconfig3
1 files changed, 1 insertions, 2 deletions
diff --git a/.editorconfig b/.editorconfig
index a7ed4a83a..06dbe065b 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -24,5 +24,4 @@ indent_style = tab
indent_style = tab
[test/**.css]
-indent_style = space
-indent_size = 8
+indent_style = tab
ltipleFO2PDF.java?h=Temp_PDF_in_PDF&id=9263449d44a00d7f223d47a83630abb4bd5aaf84'>plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */
 
package embedding;

// Java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

// FOP
import org.apache.commons.io.IOUtils;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FormattingResults;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.apps.PageSequenceResults;

/**
 * This class demonstrates the conversion of multiple FO files to PDF using FOP.
 * The FopFactory is reused. Its configuration is applied to each rendering run.
 * The FOUserAgent and Fop are newly created by the FopFactory for each run.
 * The FOUserAgent can be configured differently for each run.
 */
public class MultipleFO2PDF {

    // configure fopFactory as desired
    private FopFactory fopFactory = FopFactory.newInstance();

    // JAXP TransformerFactory can be reused, too
    private TransformerFactory factory = TransformerFactory.newInstance();
    
    /**
     * Converts an FO file to a PDF file using FOP
     * @param fo the FO file
     * @param pdf the target PDF file
     * @throws TransformerException in case of a transformation problem 
     * @throws IOException in case of an I/O problem
     * @throws FOPException in case of a FOP problem
     * @return the formatting results of the run
     */
    public FormattingResults convertFO2PDF(File fo, File pdf) 
        throws TransformerException, IOException, FOPException {
        
        OutputStream out = null;
        Fop fop;
        
        try {
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired
    
            // Setup output stream.  Note: Using BufferedOutputStream
            // for performance reasons (helpful with FileOutputStreams).
            out = new FileOutputStream(pdf);
            out = new BufferedOutputStream(out);

            // Construct fop with desired output format and output stream
            fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup JAXP using identity transformer
            Transformer transformer = factory.newTransformer(); // identity transformer
            
            // Setup input stream
            Source src = new StreamSource(fo);

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            
            // Start XSLT transformation and FOP processing
            transformer.transform(src, res);
        } finally {
            IOUtils.closeQuietly(out);
        }

        return fop.getResults();
    }

    /** 
     * Listens on standard in for names of fo files to be transformed to pdf.
     * 'quit' or the null string (for piped input) cause the listener to stop listening.
     */
    public void listen() {

        //Setup directories
        File baseDir = new File(".");
        File outDir = new File(baseDir, "out");
        outDir.mkdirs();
        BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
        
        while (true) {
            try {
                // Listen for the input file name            
                System.out.print("Input XSL-FO file ('quit' to stop): ");
                String foname = in.readLine();
                if (foname == null) {
                    System.out.println("Null input, quitting");
                    return;
                }
                foname.trim();
                if (foname.equals("quit")) {
                    System.out.println("Quitting");
                    return;
                }
                File fofile = new File(baseDir, foname);
                String pdfname = foname;
                int p = foname.lastIndexOf('.');
                pdfname = foname.substring(0, p) + ".pdf";
                File pdffile = new File(outDir, pdfname);

                // transform and render
                System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
                FormattingResults foResults = convertFO2PDF(fofile, pdffile);
                System.out.println("done!");

                // Result processing
                java.util.List pageSequences = foResults.getPageSequences();
                for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
                    PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
                    System.out.println("PageSequence " 
                            + (String.valueOf(pageSequenceResults.getID()).length() > 0 
                                    ? pageSequenceResults.getID() : "<no id>") 
                            + " generated " + pageSequenceResults.getPageCount() + " pages.");
                }
                System.out.println("Generated " + foResults.getPageCount() + " pages in total.");

            } catch (Exception e) {
                System.out.println("failure!");
                e.printStackTrace(System.out);
            } finally {
                System.out.println("");
            }
        }
    }
    
    /**
     * Main method. Set up the listener.
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        System.out.println("FOP MultipleFO2PDF\n");
        System.out.println("Preparing...");
        MultipleFO2PDF m = new MultipleFO2PDF();
        m.listen();
    }

}