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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 1999-2004 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.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. //SAX
  24. import org.xml.sax.InputSource;
  25. //Avalon
  26. import org.apache.avalon.framework.ExceptionUtil;
  27. import org.apache.avalon.framework.logger.Logger;
  28. import org.apache.avalon.framework.logger.ConsoleLogger;
  29. //FOP
  30. import org.apache.fop.apps.Driver;
  31. import org.apache.fop.apps.FOPException;
  32. /**
  33. * This class demonstrates the conversion of an FO file to PDF using FOP.
  34. */
  35. public class ExampleFO2PDF {
  36. /**
  37. * Converts an FO file to a PDF file using FOP
  38. * @param fo the FO file
  39. * @param pdf the target PDF file
  40. * @throws IOException In case of an I/O problem
  41. * @throws FOPException In case of a FOP problem
  42. */
  43. public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException {
  44. //Construct driver
  45. Driver driver = new Driver();
  46. //Setup logger
  47. Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  48. driver.enableLogging(logger);
  49. driver.initialize();
  50. //Setup Renderer (output format)
  51. driver.setRenderer(Driver.RENDER_PDF);
  52. //Setup output
  53. OutputStream out = new java.io.FileOutputStream(pdf);
  54. out = new java.io.BufferedOutputStream(out);
  55. try {
  56. driver.setOutputStream(out);
  57. //Setup input
  58. InputStream in = new java.io.FileInputStream(fo);
  59. try {
  60. driver.setInputSource(new InputSource(in));
  61. //Process FO
  62. driver.run();
  63. } finally {
  64. in.close();
  65. }
  66. } finally {
  67. out.close();
  68. }
  69. }
  70. /**
  71. * Main method.
  72. * @param args command-line arguments
  73. */
  74. public static void main(String[] args) {
  75. try {
  76. System.out.println("FOP ExampleFO2PDF\n");
  77. System.out.println("Preparing...");
  78. //Setup directories
  79. File baseDir = new File(".");
  80. File outDir = new File(baseDir, "out");
  81. outDir.mkdirs();
  82. //Setup input and output files
  83. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  84. File pdffile = new File(outDir, "ResultFO2PDF.pdf");
  85. System.out.println("Input: XSL-FO (" + fofile + ")");
  86. System.out.println("Output: PDF (" + pdffile + ")");
  87. System.out.println();
  88. System.out.println("Transforming...");
  89. ExampleFO2PDF app = new ExampleFO2PDF();
  90. app.convertFO2PDF(fofile, pdffile);
  91. System.out.println("Success!");
  92. } catch (Exception e) {
  93. System.err.println(ExceptionUtil.printStackTrace(e));
  94. System.exit(-1);
  95. }
  96. }
  97. }