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.

ExampleXML2PDF.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 1999-2005 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.OutputStream;
  21. //JAXP
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.stream.StreamSource;
  27. import javax.xml.transform.sax.SAXResult;
  28. //FOP
  29. import org.apache.fop.apps.Fop;
  30. import org.apache.fop.apps.MimeConstants;
  31. /**
  32. * This class demonstrates the conversion of an XML file to PDF using
  33. * JAXP (XSLT) and FOP (XSL-FO).
  34. */
  35. public class ExampleXML2PDF {
  36. /**
  37. * Main method.
  38. * @param args command-line arguments
  39. */
  40. public static void main(String[] args) {
  41. try {
  42. System.out.println("FOP ExampleXML2PDF\n");
  43. System.out.println("Preparing...");
  44. // Setup directories
  45. File baseDir = new File(".");
  46. File outDir = new File(baseDir, "out");
  47. outDir.mkdirs();
  48. // Setup input and output files
  49. File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
  50. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  51. File pdffile = new File(outDir, "ResultXML2PDF.pdf");
  52. System.out.println("Input: XML (" + xmlfile + ")");
  53. System.out.println("Stylesheet: " + xsltfile);
  54. System.out.println("Output: PDF (" + pdffile + ")");
  55. System.out.println();
  56. System.out.println("Transforming...");
  57. // Construct fop with desired output format
  58. Fop fop = new Fop(MimeConstants.MIME_PDF);
  59. // Setup output
  60. OutputStream out = new java.io.FileOutputStream(pdffile);
  61. out = new java.io.BufferedOutputStream(out);
  62. try {
  63. fop.setOutputStream(out);
  64. // Setup XSLT
  65. TransformerFactory factory = TransformerFactory.newInstance();
  66. Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
  67. // Set the value of a <param> in the stylesheet
  68. transformer.setParameter("versionParam", "2.0");
  69. // Setup input for XSLT transformation
  70. Source src = new StreamSource(xmlfile);
  71. // Resulting SAX events (the generated FO) must be piped through to FOP
  72. Result res = new SAXResult(fop.getDefaultHandler());
  73. // Start XSLT transformation and FOP processing
  74. transformer.transform(src, res);
  75. } finally {
  76. out.close();
  77. }
  78. System.out.println("Success!");
  79. } catch (Exception e) {
  80. e.printStackTrace(System.err);
  81. System.exit(-1);
  82. }
  83. }
  84. }