You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleFO2PDF.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package embedding;
  19. // Java
  20. import java.io.BufferedOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. //JAXP
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.Source;
  29. import javax.xml.transform.Result;
  30. import javax.xml.transform.stream.StreamSource;
  31. import javax.xml.transform.sax.SAXResult;
  32. // FOP
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FOPException;
  36. import org.apache.fop.apps.FopFactory;
  37. import org.apache.fop.apps.FormattingResults;
  38. import org.apache.fop.apps.MimeConstants;
  39. import org.apache.fop.apps.PageSequenceResults;
  40. /**
  41. * This class demonstrates the conversion of an FO file to PDF using FOP.
  42. */
  43. public class ExampleFO2PDF {
  44. // configure fopFactory as desired
  45. private FopFactory fopFactory = FopFactory.newInstance();
  46. /**
  47. * Converts an FO file to a PDF file using FOP
  48. * @param fo the FO file
  49. * @param pdf the target PDF file
  50. * @throws IOException In case of an I/O problem
  51. * @throws FOPException In case of a FOP problem
  52. */
  53. public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {
  54. OutputStream out = null;
  55. try {
  56. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  57. // configure foUserAgent as desired
  58. // Setup output stream. Note: Using BufferedOutputStream
  59. // for performance reasons (helpful with FileOutputStreams).
  60. out = new FileOutputStream(pdf);
  61. out = new BufferedOutputStream(out);
  62. // Construct fop with desired output format
  63. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  64. // Setup JAXP using identity transformer
  65. TransformerFactory factory = TransformerFactory.newInstance();
  66. Transformer transformer = factory.newTransformer(); // identity transformer
  67. // Setup input stream
  68. Source src = new StreamSource(fo);
  69. // Resulting SAX events (the generated FO) must be piped through to FOP
  70. Result res = new SAXResult(fop.getDefaultHandler());
  71. // Start XSLT transformation and FOP processing
  72. transformer.transform(src, res);
  73. // Result processing
  74. FormattingResults foResults = fop.getResults();
  75. java.util.List pageSequences = foResults.getPageSequences();
  76. for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
  77. PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
  78. System.out.println("PageSequence "
  79. + (String.valueOf(pageSequenceResults.getID()).length() > 0
  80. ? pageSequenceResults.getID() : "<no id>")
  81. + " generated " + pageSequenceResults.getPageCount() + " pages.");
  82. }
  83. System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
  84. } catch (Exception e) {
  85. e.printStackTrace(System.err);
  86. System.exit(-1);
  87. } finally {
  88. out.close();
  89. }
  90. }
  91. /**
  92. * Main method.
  93. * @param args command-line arguments
  94. */
  95. public static void main(String[] args) {
  96. try {
  97. System.out.println("FOP ExampleFO2PDF\n");
  98. System.out.println("Preparing...");
  99. //Setup directories
  100. File baseDir = new File(".");
  101. File outDir = new File(baseDir, "out");
  102. outDir.mkdirs();
  103. //Setup input and output files
  104. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  105. //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
  106. File pdffile = new File(outDir, "ResultFO2PDF.pdf");
  107. System.out.println("Input: XSL-FO (" + fofile + ")");
  108. System.out.println("Output: PDF (" + pdffile + ")");
  109. System.out.println();
  110. System.out.println("Transforming...");
  111. ExampleFO2PDF app = new ExampleFO2PDF();
  112. app.convertFO2PDF(fofile, pdffile);
  113. System.out.println("Success!");
  114. } catch (Exception e) {
  115. e.printStackTrace(System.err);
  116. System.exit(-1);
  117. }
  118. }
  119. }