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

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