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

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