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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.BufferedReader;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. //JAXP
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.Source;
  31. import javax.xml.transform.Result;
  32. import javax.xml.transform.stream.StreamSource;
  33. import javax.xml.transform.sax.SAXResult;
  34. // FOP
  35. import org.apache.commons.io.IOUtils;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.Fop;
  38. import org.apache.fop.apps.FOPException;
  39. import org.apache.fop.apps.FopFactory;
  40. import org.apache.fop.apps.FormattingResults;
  41. import org.apache.fop.apps.MimeConstants;
  42. import org.apache.fop.apps.PageSequenceResults;
  43. /**
  44. * This class demonstrates the conversion of multiple FO files to PDF using FOP.
  45. * The FopFactory is reused. Its configuration is applied to each rendering run.
  46. * The FOUserAgent and Fop are newly created by the FopFactory for each run.
  47. * The FOUserAgent can be configured differently for each run.
  48. */
  49. public class MultipleFO2PDF {
  50. // configure fopFactory as desired
  51. private FopFactory fopFactory = FopFactory.newInstance();
  52. // JAXP TransformerFactory can be reused, too
  53. private TransformerFactory factory = TransformerFactory.newInstance();
  54. /**
  55. * Converts an FO file to a PDF file using FOP
  56. * @param fo the FO file
  57. * @param pdf the target PDF file
  58. * @throws TransformerException in case of a transformation problem
  59. * @throws IOException in case of an I/O problem
  60. * @throws FOPException in case of a FOP problem
  61. * @return the formatting results of the run
  62. */
  63. public FormattingResults convertFO2PDF(File fo, File pdf)
  64. throws TransformerException, IOException, FOPException {
  65. OutputStream out = null;
  66. Fop fop;
  67. try {
  68. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  69. // configure foUserAgent as desired
  70. // Setup output stream. Note: Using BufferedOutputStream
  71. // for performance reasons (helpful with FileOutputStreams).
  72. out = new FileOutputStream(pdf);
  73. out = new BufferedOutputStream(out);
  74. // Construct fop with desired output format and output stream
  75. fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  76. // Setup JAXP using identity transformer
  77. Transformer transformer = factory.newTransformer(); // identity transformer
  78. // Setup input stream
  79. Source src = new StreamSource(fo);
  80. // Resulting SAX events (the generated FO) must be piped through to FOP
  81. Result res = new SAXResult(fop.getDefaultHandler());
  82. // Start XSLT transformation and FOP processing
  83. transformer.transform(src, res);
  84. } finally {
  85. IOUtils.closeQuietly(out);
  86. }
  87. return fop.getResults();
  88. }
  89. /**
  90. * Listens on standard in for names of fo files to be transformed to pdf.
  91. * 'quit' or the null string (for piped input) cause the listener to stop listening.
  92. */
  93. public void listen() {
  94. //Setup directories
  95. File baseDir = new File(".");
  96. File outDir = new File(baseDir, "out");
  97. outDir.mkdirs();
  98. BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
  99. while (true) {
  100. try {
  101. // Listen for the input file name
  102. System.out.print("Input XSL-FO file ('quit' to stop): ");
  103. String foname = in.readLine();
  104. if (foname == null) {
  105. System.out.println("Null input, quitting");
  106. return;
  107. }
  108. foname.trim();
  109. if (foname.equals("quit")) {
  110. System.out.println("Quitting");
  111. return;
  112. }
  113. File fofile = new File(baseDir, foname);
  114. String pdfname = foname;
  115. int p = foname.lastIndexOf('.');
  116. pdfname = foname.substring(0, p) + ".pdf";
  117. File pdffile = new File(outDir, pdfname);
  118. // transform and render
  119. System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
  120. FormattingResults foResults = convertFO2PDF(fofile, pdffile);
  121. System.out.println("done!");
  122. // Result processing
  123. java.util.List pageSequences = foResults.getPageSequences();
  124. for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
  125. PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
  126. System.out.println("PageSequence "
  127. + (String.valueOf(pageSequenceResults.getID()).length() > 0
  128. ? pageSequenceResults.getID() : "<no id>")
  129. + " generated " + pageSequenceResults.getPageCount() + " pages.");
  130. }
  131. System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
  132. } catch (Exception e) {
  133. System.out.println("failure!");
  134. e.printStackTrace(System.out);
  135. } finally {
  136. System.out.println("");
  137. }
  138. }
  139. }
  140. /**
  141. * Main method. Set up the listener.
  142. * @param args command-line arguments
  143. */
  144. public static void main(String[] args) {
  145. System.out.println("FOP MultipleFO2PDF\n");
  146. System.out.println("Preparing...");
  147. MultipleFO2PDF m = new MultipleFO2PDF();
  148. m.listen();
  149. }
  150. }