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

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