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

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