Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ExampleFO2PDFUsingSAXParser.java 4.5KB

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