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.

ExampleObj2PDF.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 1999-2004 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. import java.io.IOException;
  22. //JAXP
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Result;
  28. import javax.xml.transform.stream.StreamSource;
  29. import javax.xml.transform.sax.SAXResult;
  30. //Avalon
  31. import org.apache.avalon.framework.ExceptionUtil;
  32. import org.apache.avalon.framework.logger.ConsoleLogger;
  33. import org.apache.avalon.framework.logger.Logger;
  34. //FOP
  35. import org.apache.fop.apps.Driver;
  36. import org.apache.fop.apps.FOPException;
  37. import embedding.model.ProjectTeam;
  38. /**
  39. * This class demonstrates the conversion of an arbitrary object file to a
  40. * PDF using JAXP (XSLT) and FOP (XSL:FO).
  41. */
  42. public class ExampleObj2PDF {
  43. /**
  44. * Converts a ProjectTeam object to a PDF file.
  45. * @param team the ProjectTeam object
  46. * @param xslt the stylesheet file
  47. * @param pdf the target PDF file
  48. * @throws IOException In case of an I/O problem
  49. * @throws FOPException In case of a FOP problem
  50. * @throws TransformerException In case of a XSL transformation problem
  51. */
  52. public void convertProjectTeam2PDF(ProjectTeam team, File xslt, File pdf)
  53. throws IOException, FOPException, TransformerException {
  54. //Construct driver
  55. Driver driver = new Driver();
  56. //Setup logger
  57. Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  58. driver.enableLogging(logger);
  59. driver.initialize();
  60. //Setup Renderer (output format)
  61. driver.setRenderer(Driver.RENDER_PDF);
  62. //Setup output
  63. OutputStream out = new java.io.FileOutputStream(pdf);
  64. out = new java.io.BufferedOutputStream(out);
  65. try {
  66. driver.setOutputStream(out);
  67. //Setup XSLT
  68. TransformerFactory factory = TransformerFactory.newInstance();
  69. Transformer transformer = factory.newTransformer(new StreamSource(xslt));
  70. //Setup input for XSLT transformation
  71. Source src = team.getSourceForProjectTeam();
  72. //Resulting SAX events (the generated FO) must be piped through to FOP
  73. Result res = new SAXResult(driver.getContentHandler());
  74. //Start XSLT transformation and FOP processing
  75. transformer.transform(src, res);
  76. } finally {
  77. out.close();
  78. }
  79. }
  80. /**
  81. * Main method.
  82. * @param args command-line arguments
  83. */
  84. public static void main(String[] args) {
  85. try {
  86. System.out.println("FOP ExampleObj2PDF\n");
  87. System.out.println("Preparing...");
  88. //Setup directories
  89. File baseDir = new File(".");
  90. File outDir = new File(baseDir, "out");
  91. outDir.mkdirs();
  92. //Setup input and output
  93. File xsltfile = new File(baseDir, "xml/xslt/projectteam2FO.xsl");
  94. File pdffile = new File(outDir, "ResultObj2PDF.pdf");
  95. System.out.println("Input: a ProjectTeam object");
  96. System.out.println("Stylesheet: " + xsltfile);
  97. System.out.println("Output: PDF (" + pdffile + ")");
  98. System.out.println();
  99. System.out.println("Transforming...");
  100. ExampleObj2PDF app = new ExampleObj2PDF();
  101. app.convertProjectTeam2PDF(ExampleObj2XML.createSampleProjectTeam(), xsltfile, pdffile);
  102. System.out.println("Success!");
  103. } catch (Exception e) {
  104. System.err.println(ExceptionUtil.printStackTrace(e));
  105. System.exit(-1);
  106. }
  107. }
  108. }