Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ExampleFO2PDFUsingSAXParser.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.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.Fop;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.MimeConstants;
  36. /**
  37. * This class demonstrates the conversion of an FO file to PDF using FOP.
  38. * It uses a SAXParser with FOP as the DefaultHandler
  39. */
  40. public class ExampleFO2PDFUsingSAXParser {
  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 FactoryConfigurationError
  46. * @throws ParserConfigurationException
  47. * @throws SAXException
  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)
  52. throws FactoryConfigurationError,
  53. ParserConfigurationException,
  54. FOPException, SAXException, IOException {
  55. OutputStream out = null;
  56. try {
  57. // Construct fop and setup output format
  58. Fop fop = new Fop(MimeConstants.MIME_PDF);
  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. fop.setOutputStream(out);
  64. // Setup SAX parser
  65. // throws FactoryConfigurationError
  66. SAXParserFactory factory = SAXParserFactory.newInstance();
  67. factory.setNamespaceAware(true);
  68. // throws ParserConfigurationException
  69. SAXParser parser = factory.newSAXParser();
  70. // Obtain FOP's DefaultHandler
  71. // throws FOPException
  72. DefaultHandler dh = fop.getDefaultHandler();
  73. // Start parsing and FOP processing
  74. // throws SAXException, IOException
  75. parser.parse(fo, dh);
  76. } finally {
  77. out.close();
  78. }
  79. }
  80. /**
  81. * Main method.
  82. * @param args command-line arguments
  83. */
  84. public static void main(String[] args) {
  85. try {
  86. System.out.println("FOP ExampleFO2PDFUsingSAXParser\n");
  87. System.out.println("Preparing...");
  88. //Setup directories
  89. File baseDir = new File(".");
  90. File outDir = new File(baseDir, "out");
  91. outDir.mkdirs();
  92. //Setup input and output files
  93. File fofile = new File(baseDir, "xml/fo/helloworld.fo");
  94. File pdffile = new File(outDir, "ResultFO2PDFUsingSAXParser.pdf");
  95. System.out.println("Input: XSL-FO (" + fofile + ")");
  96. System.out.println("Output: PDF (" + pdffile + ")");
  97. System.out.println();
  98. System.out.println("Transforming...");
  99. ExampleFO2PDFUsingSAXParser app = new ExampleFO2PDFUsingSAXParser();
  100. app.convertFO2PDF(fofile, pdffile);
  101. System.out.println("Success!");
  102. } catch (Exception e) {
  103. e.printStackTrace(System.err);
  104. System.exit(-1);
  105. }
  106. }
  107. }