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

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