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.

MultipleFO2PDF.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding;
  18. // Java
  19. import java.io.BufferedOutputStream;
  20. import java.io.BufferedReader;
  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.TransformerException;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.Source;
  30. import javax.xml.transform.Result;
  31. import javax.xml.transform.stream.StreamSource;
  32. import javax.xml.transform.sax.SAXResult;
  33. // FOP
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.apps.Fop;
  36. import org.apache.fop.apps.FOPException;
  37. import org.apache.fop.apps.FopFactory;
  38. import org.apache.fop.apps.FormattingResults;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.apps.PageSequenceResults;
  41. /**
  42. * This class demonstrates the conversion of multiple FO files to PDF using FOP.
  43. * The FopFactory is reused. Its configuration is applied to each rendering run.
  44. * The FOUserAgent and Fop are newly created by the FopFactory for each run.
  45. * The FOUserAgent can be configured differently for each run.
  46. */
  47. public class MultipleFO2PDF {
  48. // configure fopFactory as desired
  49. private FopFactory fopFactory = FopFactory.newInstance();
  50. // JAXP TransformerFactory can be reused, too
  51. private TransformerFactory factory = TransformerFactory.newInstance();
  52. /**
  53. * Converts an FO file to a PDF file using FOP
  54. * @param fo the FO file
  55. * @param pdf the target PDF file
  56. * @throws TransformerException in case of a transformation problem
  57. * @throws IOException in case of an I/O problem
  58. * @throws FOPException in case of a FOP problem
  59. * @return the formatting results of the run
  60. */
  61. public FormattingResults convertFO2PDF(File fo, File pdf)
  62. throws TransformerException, IOException, FOPException {
  63. OutputStream out = null;
  64. Fop fop;
  65. try {
  66. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  67. // configure foUserAgent as desired
  68. // Setup output stream. Note: Using BufferedOutputStream
  69. // for performance reasons (helpful with FileOutputStreams).
  70. out = new FileOutputStream(pdf);
  71. out = new BufferedOutputStream(out);
  72. // Construct fop with desired output format and output stream
  73. fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  74. // Setup JAXP using identity transformer
  75. Transformer transformer = factory.newTransformer(); // identity transformer
  76. // Setup input stream
  77. Source src = new StreamSource(fo);
  78. // Resulting SAX events (the generated FO) must be piped through to FOP
  79. Result res = new SAXResult(fop.getDefaultHandler());
  80. // Start XSLT transformation and FOP processing
  81. transformer.transform(src, res);
  82. } finally {
  83. out.close();
  84. }
  85. return fop.getResults();
  86. }
  87. /**
  88. * Listens on standard in for names of fo files to be transformed to pdf.
  89. * 'quit' or the null string (for piped input) cause the listener to stop listening.
  90. */
  91. public void listen() {
  92. //Setup directories
  93. File baseDir = new File(".");
  94. File outDir = new File(baseDir, "out");
  95. outDir.mkdirs();
  96. BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
  97. while (true) {
  98. try {
  99. // Listen for the input file name
  100. System.out.print("Input XSL-FO file ('quit' to stop): ");
  101. String foname = in.readLine();
  102. if (foname == null) {
  103. System.out.println("Null input, quitting");
  104. return;
  105. }
  106. foname.trim();
  107. if (foname.equals("quit")) {
  108. System.out.println("Quitting");
  109. return;
  110. }
  111. File fofile = new File(baseDir, foname);
  112. String pdfname = foname;
  113. pdfname.replaceFirst("\\.fo", ".pdf");
  114. File pdffile = new File(outDir, pdfname);
  115. // transform and render
  116. System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
  117. FormattingResults foResults = convertFO2PDF(fofile, pdffile);
  118. System.out.println("done!");
  119. // Result processing
  120. java.util.List pageSequences = foResults.getPageSequences();
  121. for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
  122. PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
  123. System.out.println("PageSequence "
  124. + (String.valueOf(pageSequenceResults.getID()).length() > 0
  125. ? pageSequenceResults.getID() : "<no id>")
  126. + " generated " + pageSequenceResults.getPageCount() + " pages.");
  127. }
  128. System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
  129. } catch (Exception e) {
  130. System.out.println("failure!");
  131. e.printStackTrace(System.out);
  132. } finally {
  133. System.out.println("");
  134. }
  135. }
  136. }
  137. /**
  138. * Main method. Set up the listener.
  139. * @param args command-line arguments
  140. */
  141. public static void main(String[] args) {
  142. System.out.println("FOP MultipleFO2PDF\n");
  143. System.out.println("Preparing...");
  144. MultipleFO2PDF m = new MultipleFO2PDF();
  145. m.listen();
  146. }
  147. }