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

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