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

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