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

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