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

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