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

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