123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /*
- * Copyright 1999-2006 The Apache Software Foundation.
- *
- * Licensed 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.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
-
- //JAXP
- import javax.xml.transform.Transformer;
- 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.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 an FO file to PDF using FOP.
- */
- public class ExampleFO2PDF {
-
- // configure fopFactory as desired
- private FopFactory fopFactory = FopFactory.newInstance();
-
- /**
- * Converts an FO file to a PDF file using FOP
- * @param fo the FO file
- * @param pdf the target PDF file
- * @throws IOException In case of an I/O problem
- * @throws FOPException In case of a FOP problem
- */
- public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {
-
- OutputStream out = null;
-
- 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
- Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
-
- // Setup JAXP using identity transformer
- TransformerFactory factory = TransformerFactory.newInstance();
- 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);
-
- // Result processing
- FormattingResults foResults = fop.getResults();
- 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) {
- e.printStackTrace(System.err);
- System.exit(-1);
- } finally {
- out.close();
- }
- }
-
-
- /**
- * Main method.
- * @param args command-line arguments
- */
- public static void main(String[] args) {
- try {
- System.out.println("FOP ExampleFO2PDF\n");
- System.out.println("Preparing...");
-
- //Setup directories
- File baseDir = new File(".");
- File outDir = new File(baseDir, "out");
- outDir.mkdirs();
-
- //Setup input and output files
- File fofile = new File(baseDir, "xml/fo/helloworld.fo");
- //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
- File pdffile = new File(outDir, "ResultFO2PDF.pdf");
-
- System.out.println("Input: XSL-FO (" + fofile + ")");
- System.out.println("Output: PDF (" + pdffile + ")");
- System.out.println();
- System.out.println("Transforming...");
-
- ExampleFO2PDF app = new ExampleFO2PDF();
- app.convertFO2PDF(fofile, pdffile);
-
- System.out.println("Success!");
- } catch (Exception e) {
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- }
|