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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. import javax.xml.transform.Result;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Transformer;
  29. import javax.xml.transform.TransformerException;
  30. import javax.xml.transform.TransformerFactory;
  31. import javax.xml.transform.sax.SAXResult;
  32. import javax.xml.transform.stream.StreamSource;
  33. import org.apache.commons.io.IOUtils;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.apps.Fop;
  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. // JAXP TransformerFactory can be reused, too
  50. private TransformerFactory factory = TransformerFactory.newInstance();
  51. /**
  52. * Converts an FO file to a PDF file using FOP
  53. * @param fo the FO file
  54. * @param pdf the target PDF file
  55. * @throws TransformerException in case of a transformation problem
  56. * @throws IOException in case of an I/O problem
  57. * @throws FOPException in case of a FOP problem
  58. * @return the formatting results of the run
  59. */
  60. public FormattingResults convertFO2PDF(File fo, File pdf)
  61. throws TransformerException, IOException, FOPException {
  62. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  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. IOUtils.closeQuietly(out);
  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. int p = foname.lastIndexOf('.');
  114. pdfname = foname.substring(0, p) + ".pdf";
  115. File pdffile = new File(outDir, pdfname);
  116. // transform and render
  117. System.out.print("Transforming " + fofile + " to PDF file " + pdffile + "...");
  118. FormattingResults foResults = convertFO2PDF(fofile, pdffile);
  119. System.out.println("done!");
  120. // Result processing
  121. java.util.List pageSequences = foResults.getPageSequences();
  122. for (Object pageSequence : pageSequences) {
  123. PageSequenceResults pageSequenceResults = (PageSequenceResults) pageSequence;
  124. System.out.println("PageSequence "
  125. + (String.valueOf(pageSequenceResults.getID()).length() > 0
  126. ? pageSequenceResults.getID() : "<no id>")
  127. + " generated " + pageSequenceResults.getPageCount() + " pages.");
  128. }
  129. System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
  130. } catch (Exception e) {
  131. System.out.println("failure!");
  132. e.printStackTrace(System.out);
  133. } finally {
  134. System.out.println("");
  135. }
  136. }
  137. }
  138. /**
  139. * Main method. Set up the listener.
  140. * @param args command-line arguments
  141. */
  142. public static void main(String[] args) {
  143. System.out.println("FOP MultipleFO2PDF\n");
  144. System.out.println("Preparing...");
  145. MultipleFO2PDF m = new MultipleFO2PDF();
  146. m.listen();
  147. }
  148. }