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.

ExampleConcat.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: ExampleDOM2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */
  18. package embedding.intermediate;
  19. import java.io.File;
  20. import java.io.IOException;
  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.TransformerException;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.Fop;
  32. import org.apache.fop.apps.FopFactory;
  33. import org.apache.fop.apps.MimeConstants;
  34. import org.apache.fop.area.AreaTreeModel;
  35. import org.apache.fop.area.AreaTreeParser;
  36. import org.apache.fop.area.RenderPagesModel;
  37. import org.apache.fop.fonts.FontInfo;
  38. import org.apache.fop.render.Renderer;
  39. import org.apache.fop.render.xml.XMLRenderer;
  40. import org.xml.sax.SAXException;
  41. import embedding.ExampleObj2XML;
  42. import embedding.model.ProjectMember;
  43. import embedding.model.ProjectTeam;
  44. /**
  45. * Example for the intermediate format that demonstrates the concatenation of two documents
  46. * renderered to the intermediate format. A single PDF file is generated from the two intermediate
  47. * files.
  48. */
  49. public class ExampleConcat {
  50. // configure fopFactory as desired
  51. private FopFactory fopFactory = FopFactory.newInstance();
  52. /**
  53. * Creates a sample ProjectTeam instance for this demo.
  54. * @return ProjectTeam the newly created ProjectTeam instance
  55. */
  56. public static ProjectTeam createAnotherProjectTeam() {
  57. ProjectTeam team = new ProjectTeam();
  58. team.setProjectName("The Dynamic Duo");
  59. team.addMember(new ProjectMember(
  60. "Batman", "lead", "batman@heroes.org"));
  61. team.addMember(new ProjectMember(
  62. "Robin", "aid", "robin@heroes.org"));
  63. return team;
  64. }
  65. /**
  66. * Converts an XSL-FO document to an intermediate file.
  67. * @param src the source file
  68. * @param xslt the stylesheet file
  69. * @param intermediate the target intermediate file (area tree XML)
  70. * @throws IOException In case of an I/O problem
  71. * @throws FOPException In case of a FOP problem
  72. * @throws TransformerException In case of a XSL transformation problem
  73. */
  74. public void convertToIntermediate(Source src, Source xslt, File intermediate)
  75. throws IOException, FOPException, TransformerException {
  76. //Create a user agent
  77. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  78. //Create an instance of the target renderer so the XMLRenderer can use its font setup
  79. Renderer targetRenderer = userAgent.getRendererFactory().createRenderer(
  80. userAgent, MimeConstants.MIME_PDF);
  81. //Create the XMLRenderer to create the intermediate format (area tree XML)
  82. XMLRenderer xmlRenderer = new XMLRenderer();
  83. xmlRenderer.setUserAgent(userAgent);
  84. //Tell the XMLRenderer to mimic the target renderer
  85. xmlRenderer.mimicRenderer(targetRenderer);
  86. //Make sure the prepared XMLRenderer is used
  87. userAgent.setRendererOverride(xmlRenderer);
  88. // Setup output
  89. OutputStream out = new java.io.FileOutputStream(intermediate);
  90. out = new java.io.BufferedOutputStream(out);
  91. try {
  92. // Construct fop (the MIME type here is unimportant due to the override
  93. // on the user agent)
  94. Fop fop = fopFactory.newFop(null, userAgent, out);
  95. // Setup XSLT
  96. TransformerFactory factory = TransformerFactory.newInstance();
  97. Transformer transformer;
  98. if (xslt != null) {
  99. transformer = factory.newTransformer(xslt);
  100. } else {
  101. transformer = factory.newTransformer();
  102. }
  103. // Resulting SAX events (the generated FO) must be piped through to FOP
  104. Result res = new SAXResult(fop.getDefaultHandler());
  105. // Start XSLT transformation and FOP processing
  106. transformer.transform(src, res);
  107. } finally {
  108. out.close();
  109. }
  110. }
  111. /**
  112. * Concatenates an array of intermediate files to a single PDF file.
  113. * @param files the array of intermediate files (area tree XML)
  114. * @param pdffile the target PDF file
  115. * @throws IOException In case of an I/O problem
  116. * @throws TransformerException In case of a XSL transformation problem
  117. * @throws SAXException In case of an XML-related problem
  118. */
  119. public void concatToPDF(File[] files, File pdffile)
  120. throws IOException, TransformerException, SAXException {
  121. // Setup output
  122. OutputStream out = new java.io.FileOutputStream(pdffile);
  123. out = new java.io.BufferedOutputStream(out);
  124. try {
  125. //Setup fonts and user agent
  126. FontInfo fontInfo = new FontInfo();
  127. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  128. //Construct the AreaTreeModel that will received the individual pages
  129. AreaTreeModel treeModel = new RenderPagesModel(userAgent,
  130. MimeConstants.MIME_PDF, fontInfo, out);
  131. //Iterate over all intermediate files
  132. AreaTreeParser parser = new AreaTreeParser();
  133. for (int i = 0; i < files.length; i++) {
  134. Source src = new StreamSource(files[i]);
  135. parser.parse(src, treeModel, userAgent);
  136. }
  137. //Signal the end of the processing. The renderer can finalize the target document.
  138. treeModel.endDocument();
  139. } finally {
  140. out.close();
  141. }
  142. }
  143. /**
  144. * Main method.
  145. * @param args command-line arguments
  146. */
  147. public static void main(String[] args) {
  148. try {
  149. System.out.println("FOP ExampleConcat\n");
  150. //Setup directories
  151. File baseDir = new File(".");
  152. File outDir = new File(baseDir, "out");
  153. outDir.mkdirs();
  154. //Setup output file
  155. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  156. File[] files = new File[] {
  157. new File(outDir, "team1.at.xml"),
  158. new File(outDir, "team2.at.xml")};
  159. File pdffile = new File(outDir, "ResultConcat.pdf");
  160. for (int i = 0; i < files.length; i++) {
  161. System.out.println("Intermediate file " + (i + 1) + ": "
  162. + files[i].getCanonicalPath());
  163. }
  164. System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
  165. System.out.println();
  166. ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
  167. ProjectTeam team2 = createAnotherProjectTeam();
  168. ExampleConcat app = new ExampleConcat();
  169. //Create intermediate files
  170. app.convertToIntermediate(
  171. team1.getSourceForProjectTeam(),
  172. new StreamSource(xsltfile), files[0]);
  173. app.convertToIntermediate(
  174. team2.getSourceForProjectTeam(),
  175. new StreamSource(xsltfile), files[1]);
  176. //Concatenate the individual intermediate files to one document
  177. app.concatToPDF(files, pdffile);
  178. System.out.println("Success!");
  179. } catch (Exception e) {
  180. e.printStackTrace(System.err);
  181. System.exit(-1);
  182. }
  183. }
  184. }