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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 1999-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.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. //JAXP
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Result;
  29. import javax.xml.transform.stream.StreamSource;
  30. import javax.xml.transform.sax.SAXResult;
  31. // FOP
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.FormattingResults;
  37. import org.apache.fop.apps.MimeConstants;
  38. import org.apache.fop.apps.PageSequenceResults;
  39. /**
  40. * This class demonstrates the conversion of an FO file to PDF using FOP.
  41. */
  42. public class ExampleFO2PDF {
  43. // configure fopFactory as desired
  44. private FopFactory fopFactory = FopFactory.newInstance();
  45. /**
  46. * Converts an FO file to a PDF file using FOP
  47. * @param fo the FO file
  48. * @param pdf the target PDF file
  49. * @throws IOException In case of an I/O problem
  50. * @throws FOPException In case of a FOP problem
  51. */
  52. public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {
  53. OutputStream out = null;
  54. try {
  55. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  56. // configure foUserAgent as desired
  57. // Setup output stream. Note: Using BufferedOutputStream
  58. // for performance reasons (helpful with FileOutputStreams).
  59. out = new FileOutputStream(pdf);
  60. out = new BufferedOutputStream(out);
  61. // Construct fop with desired output format
  62. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  63. // Setup JAXP using identity transformer
  64. TransformerFactory factory = TransformerFactory.newInstance();
  65. Transformer transformer = factory.newTransformer(); // identity transformer
  66. // Setup input stream
  67. Source src = new StreamSource(fo);
  68. // Resulting SAX events (the generated FO) must be piped through to FOP
  69. Result res = new SAXResult(fop.getDefaultHandler());
  70. // Start XSLT transformation and FOP processing
  71. transformer.transform(src, res);
  72. // Result processing
  73. FormattingResults foResults = fop.getResults();
  74. java.util.List pageSequences = foResults.getPageSequences();
  75. for (java.util.Iterator it = pageSequences.iterator(); it.hasNext();) {
  76. PageSequenceResults pageSequenceResults = (PageSequenceResults)it.next();
  77. System.out.println("PageSequence "
  78. + (String.valueOf(pageSequenceResults.getID()).length() > 0
  79. ? pageSequenceResults.getID() : "<no id>")
  80. + " generated " + pageSequenceResults.getPageCount() + " pages.");
  81. }
  82. System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
  83. } catch (Exception e) {
  84. e.printStackTrace(System.err);
  85. System.exit(-1);
  86. } finally {
  87. out.close();
  88. }
  89. }
  90. /**
  91. * Main method.
  92. * @param args command-line arguments
  93. */
  94. public static void main(String[] args) {
  95. try {
  96. System.out.println("FOP ExampleFO2PDF\n");
  97. System.out.println("Preparing...");
  98. //Setup directories
  99. File baseDir = new File(".");
  100. File outDir = new File(baseDir, "out");
  101. outDir.mkdirs();
  102. //Setup input and output files
  103. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  104. //File fofile = new File(baseDir, "../fo/pagination/franklin_2pageseqs.fo");
  105. File pdffile = new File(outDir, "ResultFO2PDF.pdf");
  106. System.out.println("Input: XSL-FO (" + fofile + ")");
  107. System.out.println("Output: PDF (" + pdffile + ")");
  108. System.out.println();
  109. System.out.println("Transforming...");
  110. ExampleFO2PDF app = new ExampleFO2PDF();
  111. app.convertFO2PDF(fofile, pdffile);
  112. System.out.println("Success!");
  113. } catch (Exception e) {
  114. e.printStackTrace(System.err);
  115. System.exit(-1);
  116. }
  117. }
  118. }