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.

ExampleFO2PDFUsingSAXParser.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.parsers.FactoryConfigurationError;
  26. import javax.xml.parsers.ParserConfigurationException;
  27. import javax.xml.parsers.SAXParser;
  28. import javax.xml.parsers.SAXParserFactory;
  29. import org.xml.sax.SAXException;
  30. import org.xml.sax.helpers.DefaultHandler;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FopFactory;
  34. import org.apache.fop.apps.MimeConstants;
  35. /**
  36. * This class demonstrates the conversion of an FO file to PDF using FOP.
  37. * It uses a SAXParser with FOP as the DefaultHandler
  38. */
  39. public class ExampleFO2PDFUsingSAXParser {
  40. // configure fopFactory as desired
  41. private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  42. /**
  43. * Converts an FO file to a PDF file using FOP
  44. * @param fo the FO file
  45. * @param pdf the target PDF file
  46. * @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration
  47. * @throws ParserConfigurationException In case of a problem with the parser configuration
  48. * @throws SAXException In case of a problem during XML processing
  49. * @throws IOException In case of an I/O problem
  50. */
  51. public void convertFO2PDF(File fo, File pdf)
  52. throws FactoryConfigurationError,
  53. ParserConfigurationException,
  54. SAXException, IOException {
  55. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  56. // configure foUserAgent as desired
  57. OutputStream out = null;
  58. try {
  59. // Setup output stream. Note: Using BufferedOutputStream
  60. // for performance reasons (helpful with FileOutputStreams).
  61. out = new FileOutputStream(pdf);
  62. out = new BufferedOutputStream(out);
  63. // Construct fop and setup output format
  64. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  65. // Setup SAX parser
  66. // throws FactoryConfigurationError
  67. SAXParserFactory factory = SAXParserFactory.newInstance();
  68. factory.setNamespaceAware(true);
  69. // throws ParserConfigurationException
  70. SAXParser parser = factory.newSAXParser();
  71. // Obtain FOP's DefaultHandler
  72. // throws FOPException
  73. DefaultHandler dh = fop.getDefaultHandler();
  74. // Start parsing and FOP processing
  75. // throws SAXException, IOException
  76. parser.parse(fo, dh);
  77. } finally {
  78. out.close();
  79. }
  80. }
  81. /**
  82. * Main method.
  83. * @param args command-line arguments
  84. */
  85. public static void main(String[] args) {
  86. try {
  87. System.out.println("FOP ExampleFO2PDFUsingSAXParser\n");
  88. System.out.println("Preparing...");
  89. //Setup directories
  90. File baseDir = new File(".");
  91. File outDir = new File(baseDir, "out");
  92. outDir.mkdirs();
  93. //Setup input and output files
  94. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  95. File pdffile = new File(outDir, "ResultFO2PDFUsingSAXParser.pdf");
  96. System.out.println("Input: XSL-FO (" + fofile + ")");
  97. System.out.println("Output: PDF (" + pdffile + ")");
  98. System.out.println();
  99. System.out.println("Transforming...");
  100. ExampleFO2PDFUsingSAXParser app = new ExampleFO2PDFUsingSAXParser();
  101. app.convertFO2PDF(fofile, pdffile);
  102. System.out.println("Success!");
  103. } catch (Exception e) {
  104. e.printStackTrace(System.err);
  105. System.exit(-1);
  106. }
  107. }
  108. }