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 4.8KB

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