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.

intermediate.xml 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Copyright 2006 The Apache Software Foundation
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. -->
  14. <!-- $Id$ -->
  15. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
  16. <document>
  17. <header>
  18. <title>Intermediate Format</title>
  19. <version>$Revision$</version>
  20. </header>
  21. <body>
  22. <note>
  23. Please note that the intermediate format is an <strong>advanced feature</strong> and can be ignored by most
  24. users of Apache FOP.
  25. </note>
  26. <section id="introduction">
  27. <title>Introduction</title>
  28. <p>
  29. The intermediate format (IF) is a proprietary XML format that represents the area tree
  30. generated by the layout engine. The area tree is conceptually defined in the
  31. <a href="http://www.w3.org/TR/xsl/slice1.html#section-N742-Formatting">XSL-FO specification in chapter 1.1.2</a>.
  32. The IF can be generated through the area tree XML Renderer (the XMLRenderer).
  33. </p>
  34. <p>
  35. The intermediate format can be used to generate intermediate documents that are modified
  36. before they are finally rendered to their ultimate output format. Modifications include
  37. adjusting and changing trait values, adding or modifying area objects, inserting prefabricated
  38. pages, overlays, imposition (n-up, rotation, scaling etc.). Multiple IF files can be combined
  39. to a single output file.
  40. </p>
  41. </section>
  42. <section id="usage">
  43. <title>Usage of the Intermediate Format</title>
  44. <p>
  45. As already mentioned, the IF is generated by using the <strong>XMLRenderer</strong> (MIME type:
  46. <strong>application/X-fop-areatree</strong>). So, you basically set the right MIME type for
  47. the output format and process your FO files as if you would create a PDF file. However, there
  48. is an important detail to consider: The various Renderers don't all use the same font sources.
  49. To be able to create the right area tree for the ultimate output file, you need to create
  50. the IF file using the right font setup. This is achieved by telling the XMLRenderer to mimic
  51. another renderer. This is done by calling the XMLRenderer's mimicRenderer() method with an
  52. instance of the ultimate target renderer as the single parameter. This has a consequence: An
  53. IF file rendered with the Java2DRenderer may not look as expected when it was actually generated
  54. for the PDF renderer. For renderers that use the same font setup, this restriction does not
  55. apply (PDF and PS, for example). Generating the intermediate format file is the first step.
  56. </p>
  57. <p>
  58. The second step is to reparse the IF file using the <strong>AreaTreeParser</strong> which is
  59. found in the org.apache.fop.area package. The pages retrieved from the IF file are added to an
  60. AreaTreeModel instance from where they are normally rendered using one of the available Renderer
  61. implementations. You can find examples for the IF processing in the
  62. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/intermediate/"><code>examples/embedding</code></a>
  63. directory in the FOP distribution
  64. </p>
  65. <p>
  66. The basic pattern to parse the IF format looks like this:
  67. </p>
  68. <source><![CDATA[
  69. // Setup output
  70. OutputStream out = new java.io.FileOutputStream(pdffile);
  71. out = new java.io.BufferedOutputStream(out);
  72. try {
  73. //Setup fonts and user agent
  74. FontInfo fontInfo = new FontInfo();
  75. FOUserAgent userAgent = new FOUserAgent();
  76. //Construct the AreaTreeModel that will received the individual pages
  77. AreaTreeModel treeModel = new RenderPagesModel(userAgent,
  78. MimeConstants.MIME_PDF, fontInfo, out);
  79. //Parse the IF file into the area tree
  80. AreaTreeParser parser = new AreaTreeParser();
  81. Source src = new StreamSource(myIFFile);
  82. parser.parse(src, treeModel, userAgent);
  83. //Signal the end of the processing. The renderer can finalize the target document.
  84. treeModel.endDocument();
  85. } finally {
  86. out.close();
  87. }]]></source>
  88. <p>
  89. This example simply reads an IF file and renders it to a PDF file. Please note, that in normal
  90. FOP operation you're shielded from having to instantiate the FontInfo object yourself. This
  91. is normally a task of the AreaTreeHandler which is not present in this scenario. The same
  92. applies to the AreaTreeModel instance, in this case an instance of a subclass called
  93. RenderPagesModel. RenderPagesModel is ideal in this case as it has very little overhead
  94. processing the individual pages. An important line in the example is the call to
  95. <code>endDocument()</code> on the AreaTreeModel. This lets the Renderer know that the processing
  96. is now finished.
  97. </p>
  98. <p>
  99. The intermediate format can also be used from the <a href="running.html#standalone-start">command-line</a>
  100. by using the "-atin" parameter for specifying the area tree XML as input file. You can also
  101. specify a "mimic renderer" by inserting a MIME type between "-at" and the output file.
  102. </p>
  103. <section id="concat">
  104. <title>Concatenating Documents</title>
  105. <p>
  106. This initial example is obviously not very useful. It would be faster to create the PDF file
  107. directly. As the <a href="http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/examples/embedding/java/embedding/intermediate/ExampleConcat.java">ExampleConcat.java</a>
  108. example shows you can easily parse multiple IF files in a row and add the parsed pages to the
  109. same AreaTreeModel instance which essentially concatenates all the input document to one single
  110. output document.
  111. </p>
  112. </section>
  113. <section id="modifying">
  114. <title>Modifying Documents</title>
  115. <p>
  116. One of the most important use cases for the intermediate format is obviously modifying the area
  117. tree XML before finally rendering it to the target format. You can easily use XSLT to process
  118. the IF file according to your needs. Please note, that we will currently not formally describe
  119. the intermediate format. You need to have a good understanding its structure so you don't
  120. create any non-parseable files. We may add an XML Schema and more detailed documentation at a
  121. later time. You're invited to help us with that.
  122. </p>
  123. </section>
  124. <section id="advanced">
  125. <title>Advanced Use</title>
  126. <p>
  127. The generation of the intermediate format as well as it parsing process has been designed to allow
  128. for maximum flexibility and optimization. Please note that you can call <code>setTransformerHandler()</code> on
  129. XMLRenderer to give the XMLRenderer your own TransformerHandler instance in case you would like to
  130. do custom serialization (to a W3C DOM, for example) and/or to directly modify the area tree using
  131. XSLT. The AreaTreeParser on the other side allows you to retrieve a ContentHandler instance where
  132. you can manually send SAX events to to start the parsing process (see <code>getContentHandler()</code>).
  133. </p>
  134. </section>
  135. </section>
  136. </body>
  137. </document>