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.

embedding.xml 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  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$ -->
  17. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
  18. <!-- Embedding FOP -->
  19. <document>
  20. <header>
  21. <title>Apache™ FOP: Embedding</title>
  22. <subtitle>How to Embed Apache™ FOP in a Java application</subtitle>
  23. <version>$Revision$</version>
  24. </header>
  25. <body>
  26. <section id="overview">
  27. <title>Overview</title>
  28. <p>
  29. Review <a href="running.html">Running Apache™ FOP</a> for important information that applies
  30. to embedded applications as well as command-line use, such as options and performance.
  31. </p>
  32. <p>
  33. To embed Apache™ FOP in your application, first create a new
  34. org.apache.fop.apps.FopFactory instance. This object can be used to launch multiple
  35. rendering runs. For each run, create a new org.apache.fop.apps.Fop instance through
  36. one of the factory methods of FopFactory. In the method call you specify which output
  37. format (i.e. Renderer) to use and, if the selected renderer requires an OutputStream,
  38. which OutputStream to use for the results of the rendering. You can customize FOP's
  39. behaviour in a rendering run by supplying your own FOUserAgent instance. The
  40. FOUserAgent can, for example, be used to set your own Renderer instance (details
  41. below). Finally, you retrieve a SAX DefaultHandler instance from the Fop object and
  42. use that as the SAXResult of your transformation.
  43. </p>
  44. <note>
  45. We recently changed FOP's outer API to what we consider the final API. This might require
  46. some changes in your application. The main reasons for these changes were performance
  47. improvements due to better reuse of reusable objects and reduced use of static variables
  48. for added flexibility in complex environments.
  49. </note>
  50. </section>
  51. <section id="basics">
  52. <title>Basic Usage Pattern</title>
  53. <p>
  54. Apache FOP relies heavily on JAXP. It uses SAX events exclusively to receive the XSL-FO
  55. input document. It is therefore a good idea that you know a few things about JAXP (which
  56. is a good skill anyway). Let's look at the basic usage pattern for FOP...
  57. </p>
  58. <p>Here is the basic pattern to render an XSL-FO file to PDF:
  59. </p>
  60. <source><![CDATA[
  61. import org.apache.fop.apps.FopFactory;
  62. import org.apache.fop.apps.Fop;
  63. import org.apache.fop.apps.MimeConstants;
  64. /*..*/
  65. // Step 1: Construct a FopFactory
  66. // (reuse if you plan to render multiple documents!)
  67. FopFactory fopFactory = FopFactory.newInstance();
  68. // Step 2: Set up output stream.
  69. // Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
  70. OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:/Temp/myfile.pdf")));
  71. try {
  72. // Step 3: Construct fop with desired output format
  73. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
  74. // Step 4: Setup JAXP using identity transformer
  75. TransformerFactory factory = TransformerFactory.newInstance();
  76. Transformer transformer = factory.newTransformer(); // identity transformer
  77. // Step 5: Setup input and output for XSLT transformation
  78. // Setup input stream
  79. Source src = new StreamSource(new File("C:/Temp/myfile.fo"));
  80. // Resulting SAX events (the generated FO) must be piped through to FOP
  81. Result res = new SAXResult(fop.getDefaultHandler());
  82. // Step 6: Start XSLT transformation and FOP processing
  83. transformer.transform(src, res);
  84. } finally {
  85. //Clean-up
  86. out.close();
  87. }]]></source>
  88. <p>
  89. Let's discuss these 5 steps in detail:
  90. </p>
  91. <ul>
  92. <li>
  93. <strong>Step 1:</strong> You create a new FopFactory instance. The FopFactory instance holds
  94. references to configuration information and cached data. It's important to reuse this
  95. instance if you plan to render multiple documents during a JVM's lifetime.
  96. </li>
  97. <li>
  98. <strong>Step 2:</strong> You set up an OutputStream that the generated document
  99. will be written to. It's a good idea to buffer the OutputStream as demonstrated
  100. to improve performance.
  101. </li>
  102. <li>
  103. <strong>Step 3:</strong> You create a new Fop instance through one of the factory
  104. methods on the FopFactory. You tell the FopFactory what your desired output format
  105. is. This is done by using the MIME type of the desired output format (ex. "application/pdf").
  106. You can use one of the MimeConstants.* constants. The second parameter is the
  107. OutputStream you've setup up in step 2.
  108. </li>
  109. <li>
  110. <strong>Step 4</strong> We recommend that you use JAXP Transformers even
  111. if you don't do XSLT transformations to generate the XSL-FO file. This way
  112. you can always use the same basic pattern. The example here sets up an
  113. "identity transformer" which just passes the input (Source) unchanged to the
  114. output (Result). You don't have to work with a SAXParser if you don't do any
  115. XSLT transformations.
  116. </li>
  117. <li>
  118. <strong>Step 5:</strong> Here you set up the input and output for the XSLT
  119. transformation. The Source object is set up to load the "myfile.fo" file.
  120. The Result is set up so the output of the XSLT transformation is sent to FOP.
  121. The FO file is sent to FOP in the form of SAX events which is the most efficient
  122. way. Please always avoid saving intermediate results to a file or a memory buffer
  123. because that affects performance negatively.
  124. </li>
  125. <li>
  126. <strong>Step 6:</strong> Finally, we start the XSLT transformation by starting
  127. the JAXP Transformer. As soon as the JAXP Transformer starts to send its output
  128. to FOP, FOP itself starts its processing in the background. When the
  129. <code>transform()</code> method returns FOP will also have finished converting
  130. the FO file to a PDF file and you can close the OutputStream.
  131. <note label="Tip!">
  132. It's a good idea to enclose the whole conversion in a try..finally statement. If
  133. you close the OutputStream in the finally section, this will make sure that the
  134. OutputStream is properly closed even if an exception occurs during the conversion.
  135. </note>
  136. </li>
  137. </ul>
  138. <p>
  139. If you're not totally familiar with JAXP Transformers, please have a look at the
  140. <a href="#examples">Embedding examples</a> below. The section contains examples
  141. for all sorts of use cases. If you look at all of them in turn you should be able
  142. to see the patterns in use and the flexibility this approach offers without adding
  143. too much complexity.
  144. </p>
  145. <p>
  146. This may look complicated at first, but it's really just the combination of an
  147. XSL transformation and a FOP run. It's also easy to comment out the FOP part
  148. for debugging purposes, for example when you're tracking down a bug in your
  149. stylesheet. You can easily write the XSL-FO output from the XSL transformation
  150. to a file to check if that part generates the expected output. An example for that
  151. can be found in the <a href="#examples">Embedding examples</a> (See "ExampleXML2FO").
  152. </p>
  153. <section id="basic-logging">
  154. <title>Logging</title>
  155. <p>
  156. Logging is now a little different than it was in FOP 0.20.5. We've switched from
  157. Avalon Logging to <a href="ext:commons-logging">Jakarta Commons Logging</a>.
  158. While with Avalon Logging the loggers were directly given to FOP, FOP now retrieves
  159. its logger(s) through a statically available LogFactory. This is similar to the
  160. general pattern that you use when you work with Apache Log4J directly, for example.
  161. We call this "static logging" (Commons Logging, Log4J) as opposed to "instance logging"
  162. (Avalon Logging). This has a consequence: You can't give FOP a logger for each
  163. processing run anymore. The log output of multiple, simultaneously running FOP instances
  164. is sent to the same logger.
  165. </p>
  166. <note>
  167. We know this may be an issue in multi-threaded server environments if you'd like to
  168. know what's going on in every single FOP processing run. We're planning to add an
  169. additional feedback facility to FOP which can be used to obtain all sorts of specific
  170. feedback (validation messages, layout problems etc.). "Static logging" is mainly
  171. interesting for a developer working on FOP and for advanced users who are debugging
  172. FOP. We don't consider the logging output to be useful to normal FOP users. Please
  173. have some patience until we can add this feature or jump in and help us build it. We've
  174. set up a <a href="http://wiki.apache.org/xmlgraphics-fop/ProcessingFeedback">Wiki page</a>
  175. which documents what we're going to build.
  176. </note>
  177. <p>
  178. By default, <a href="ext:commons-logging">Jakarta Commons Logging</a> uses
  179. JDK logging (available in JDKs 1.4 or higher) as its backend. You can configure Commons
  180. Logging to use an alternative backend, for example Log4J. Please consult the
  181. <a href="ext:commons-logging">documentation for Jakarta Commons Logging</a> on
  182. how to configure alternative backends.
  183. </p>
  184. </section>
  185. <section id="render">
  186. <title>Processing XSL-FO</title>
  187. <p>
  188. Once the Fop instance is set up, call <code>getDefaultHandler()</code> to obtain a SAX
  189. DefaultHandler instance to which you can send the SAX events making up the XSL-FO
  190. document you'd like to render. FOP processing starts as soon as the DefaultHandler's
  191. <code>startDocument()</code> method is called. Processing stops again when the
  192. DefaultHandler's <code>endDocument()</code> method is called. Please refer to the basic
  193. usage pattern shown above to render a simple XSL-FO document.
  194. </p>
  195. </section>
  196. <section id="render-with-xslt">
  197. <title>Processing XSL-FO generated from XML+XSLT</title>
  198. <p>
  199. If you want to process XSL-FO generated from XML using XSLT we recommend
  200. again using standard JAXP to do the XSLT part and piping the generated SAX
  201. events directly through to FOP. The only thing you'd change to do that
  202. on the basic usage pattern above is to set up the Transformer differently:
  203. </p>
  204. <source><![CDATA[
  205. //without XSLT:
  206. //Transformer transformer = factory.newTransformer(); // identity transformer
  207. //with XSLT:
  208. Source xslt = new StreamSource(new File("mystylesheet.xsl"));
  209. Transformer transformer = factory.newTransformer(xslt);]]></source>
  210. </section>
  211. </section>
  212. <section id="input">
  213. <title>Input Sources</title>
  214. <p>
  215. The input XSL-FO document is always received by FOP as a SAX stream (see the
  216. <a href="../dev/design/parsing.html">Parsing Design Document</a> for the rationale).
  217. </p>
  218. <p>
  219. However, you may not always have your input document available as a SAX stream.
  220. But with JAXP it's easy to convert different input sources to a SAX stream so you
  221. can pipe it into FOP. That sounds more difficult than it is. You simply have
  222. to set up the right Source instance as input for the JAXP transformation.
  223. A few examples:
  224. </p>
  225. <ul>
  226. <li>
  227. <strong>URL:</strong> <code>Source src = new StreamSource("http://localhost:8080/testfile.xml");</code>
  228. </li>
  229. <li>
  230. <strong>File:</strong> <code>Source src = new StreamSource(new File("C:/Temp/myinputfile.xml"));</code>
  231. </li>
  232. <li>
  233. <strong>String:</strong> <code>Source src = new StreamSource(new StringReader(myString)); // myString is a String</code>
  234. </li>
  235. <li>
  236. <strong>InputStream:</strong> <code>Source src = new StreamSource(new MyInputStream(something));</code>
  237. </li>
  238. <li>
  239. <strong>Byte Array:</strong> <code>Source src = new StreamSource(new ByteArrayInputStream(myBuffer)); // myBuffer is a byte[] here</code>
  240. </li>
  241. <li>
  242. <strong>DOM:</strong> <code>Source src = new DOMSource(myDocument); // myDocument is a Document or a Node</code>
  243. </li>
  244. <li>
  245. <strong>Java Objects:</strong> Please have a look at the <a href="#examples">Embedding examples</a> which contain an example for this.
  246. </li>
  247. </ul>
  248. <p>
  249. There are a variety of upstream data manipulations possible.
  250. For example, you may have a DOM and an XSL stylesheet; or you may want to
  251. set variables in the stylesheet. Interface documentation and some cookbook
  252. solutions to these situations are provided in
  253. <a href="http://xml.apache.org/xalan-j/usagepatterns.html">Xalan Basic Usage Patterns</a>.
  254. </p>
  255. </section>
  256. <section id="config-internal">
  257. <title>Configuring Apache FOP Programmatically</title>
  258. <p>
  259. Apache FOP provides two levels on which you can customize FOP's
  260. behaviour: the FopFactory and the user agent.
  261. </p>
  262. <section id="fop-factory">
  263. <title>Customizing the FopFactory</title>
  264. <p>
  265. The FopFactory holds configuration data and references to objects which are reusable over
  266. multiple rendering runs. It's important to instantiate it only once (except in special
  267. environments) and reuse it every time to create new FOUserAgent and Fop instances.
  268. </p>
  269. <p>
  270. You can set all sorts of things on the FopFactory:
  271. </p>
  272. <ul>
  273. <li>
  274. <p>
  275. The <strong>font base URL</strong> to use when resolving relative URLs for fonts. Example:
  276. </p>
  277. <source>fopFactory.setFontBaseURL("file:///C:/Temp/fonts");</source>
  278. </li>
  279. <li>
  280. <p>
  281. The <strong>hyphenation base URL</strong> to use when resolving relative URLs for
  282. hyphenation patterns. Example:
  283. </p>
  284. <source>fopFactory.setHyphenBaseURL("file:///C:/Temp/hyph");</source>
  285. </li>
  286. <li>
  287. <p>
  288. Disable <strong>strict validation</strong>. When disabled FOP is less strict about the rules
  289. established by the XSL-FO specification. Example:
  290. </p>
  291. <source>fopFactory.setStrictValidation(false);</source>
  292. </li>
  293. <li>
  294. <p>
  295. Enable an <strong>alternative set of rules for text indents</strong> that tries to mimic the behaviour of many commercial
  296. FO implementations, that chose to break the specification in this respect. The default of this option is
  297. 'false', which causes Apache FOP to behave exactly as described in the specification. To enable the
  298. alternative behaviour, call:
  299. </p>
  300. <source>fopFactory.setBreakIndentInheritanceOnReferenceAreaBoundary(true);</source>
  301. </li>
  302. <li>
  303. <p>
  304. Set the <strong>source resolution</strong> for the document. This is used internally to determine the pixel
  305. size for SVG images and bitmap images without resolution information. Default: 72 dpi. Example:
  306. </p>
  307. <source>fopFactory.setSourceResolution(96); // =96dpi (dots/pixels per Inch)</source>
  308. </li>
  309. <li>
  310. <p>
  311. Manually add an <strong>ElementMapping instance</strong>. If you want to supply a special FOP extension
  312. you can give the instance to the FOUserAgent. Normally, the FOP extensions can be automatically detected
  313. (see the documentation on extension for more info). Example:
  314. </p>
  315. <source>fopFactory.addElementMapping(myElementMapping); // myElementMapping is a org.apache.fop.fo.ElementMapping</source>
  316. </li>
  317. <li>
  318. <p>
  319. Set a <strong>URIResolver</strong> for custom URI resolution. By supplying a JAXP URIResolver you can add
  320. custom URI resolution functionality to FOP. For example, you can use
  321. <a href="ext:xml.apache.org/commons/resolver">Apache XML Commons Resolver</a> to make use of XCatalogs. Example:
  322. </p>
  323. <source>fopFactory.setURIResolver(myResolver); // myResolver is a javax.xml.transform.URIResolver</source>
  324. <note>
  325. Both the FopFactory and the FOUserAgent have a method to set a URIResolver. The URIResolver on the FopFactory
  326. is primarily used to resolve URIs on factory-level (hyphenation patterns, for example) and it is always used
  327. if no other URIResolver (for example on the FOUserAgent) resolved the URI first.
  328. </note>
  329. </li>
  330. </ul>
  331. </section>
  332. <section id="user-agent">
  333. <title>Customizing the User Agent</title>
  334. <p>
  335. The user agent is the entity that allows you to interact with a single rendering run, i.e. the processing of a single
  336. document. If you wish to customize the user agent's behaviour, the first step is to create your own instance
  337. of FOUserAgent using the appropriate factory method on FopFactory and pass that
  338. to the factory method that will create a new Fop instance:
  339. </p>
  340. <source><![CDATA[
  341. FopFactory fopFactory = FopFactory.newInstance(); // Reuse the FopFactory if possible!
  342. // do the following for each new rendering run
  343. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  344. // customize userAgent
  345. Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, userAgent, out);]]></source>
  346. <p>
  347. You can do all sorts of things on the user agent:
  348. </p>
  349. <ul>
  350. <li>
  351. <p>
  352. The <strong>base URL</strong> to use when resolving relative URLs. Example:
  353. </p>
  354. <source>userAgent.setBaseURL("file:///C:/Temp/");</source>
  355. </li>
  356. <li>
  357. <p>
  358. Set the <strong>producer</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. The default producer is "Apache FOP". Example:
  359. </p>
  360. <source>userAgent.setProducer("MyKillerApplication");</source>
  361. </li>
  362. <li>
  363. <p>
  364. Set the <strong>creating user</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. Example:
  365. </p>
  366. <source>userAgent.setCreator("John Doe");</source>
  367. </li>
  368. <li>
  369. <p>
  370. Set the <strong>author</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. Example:
  371. </p>
  372. <source>userAgent.setAuthor("John Doe");</source>
  373. </li>
  374. <li>
  375. <p>
  376. Override the <strong>creation date and time</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. Example:
  377. </p>
  378. <source>userAgent.setCreationDate(new Date());</source>
  379. </li>
  380. <li>
  381. <p>
  382. Set the <strong>title</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. Example:
  383. </p>
  384. <source>userAgent.setTitle("Invoice No 138716847");</source>
  385. </li>
  386. <li>
  387. <p>
  388. Set the <strong>keywords</strong> of the document. This is metadata information that can be used for certain output formats such as PDF. Example:
  389. </p>
  390. <source>userAgent.setKeywords("XML XSL-FO");</source>
  391. </li>
  392. <li>
  393. <p>
  394. Set the <strong>target resolution</strong> for the document. This is used to
  395. specify the output resolution for bitmap images generated by bitmap renderers
  396. (such as the TIFF renderer) and by bitmaps generated by Apache Batik for filter
  397. effects and such. Default: 72 dpi. Example:
  398. </p>
  399. <source>userAgent.setTargetResolution(300); // =300dpi (dots/pixels per Inch)</source>
  400. </li>
  401. <li>
  402. <p>
  403. Set <strong>your own Renderer instance</strong>. If you want to supply your own renderer or
  404. configure a Renderer in a special way you can give the instance to the FOUserAgent. Normally,
  405. the Renderer instance is created by FOP. Example:
  406. </p>
  407. <source>userAgent.setRendererOverride(myRenderer); // myRenderer is an org.apache.fop.render.Renderer</source>
  408. </li>
  409. <li>
  410. <p>
  411. Set <strong>your own FOEventHandler instance</strong>. If you want to supply your own FOEventHandler or
  412. configure an FOEventHandler subclass in a special way you can give the instance to the FOUserAgent. Normally,
  413. the FOEventHandler instance is created by FOP. Example:
  414. </p>
  415. <source>userAgent.setFOEventHandlerOverride(myFOEventHandler); // myFOEventHandler is an org.apache.fop.fo.FOEventHandler</source>
  416. </li>
  417. <li>
  418. <p>
  419. Set a <strong>URIResolver</strong> for custom URI resolution. By supplying a JAXP URIResolver you can add
  420. custom URI resolution functionality to FOP. For example, you can use
  421. <a href="ext:xml.apache.org/commons/resolver">Apache XML Commons Resolver</a> to make use of XCatalogs. Example:
  422. </p>
  423. <source>userAgent.setURIResolver(myResolver); // myResolver is a javax.xml.transform.URIResolver</source>
  424. <note>
  425. Both the FopFactory and the FOUserAgent have a method to set a URIResolver. The URIResolver on the FOUserAgent is
  426. used for resolving URIs which are document-related. If it's not set or cannot resolve a URI, the URIResolver
  427. from the FopFactory is used.
  428. </note>
  429. </li>
  430. </ul>
  431. <note>
  432. You should not reuse an FOUserAgent instance between FOP rendering runs although you can. Especially
  433. in multi-threaded environment, this is a bad idea.
  434. </note>
  435. </section>
  436. </section>
  437. <section id="config-external">
  438. <title>Using a Configuration File</title>
  439. <p>
  440. Instead of setting the parameters manually in code as shown above you can also set
  441. many values from an XML configuration file:
  442. </p>
  443. <source><![CDATA[
  444. import org.apache.avalon.framework.configuration.Configuration;
  445. import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  446. /*..*/
  447. DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
  448. Configuration cfg = cfgBuilder.buildFromFile(new File("C:/Temp/mycfg.xml"));
  449. fopFactory.setUserConfig(cfg);
  450. /* ..or.. */
  451. fopFactory.setUserConfig(new File("C:/Temp/mycfg.xml"));]]></source>
  452. <p>
  453. The layout of the configuration file is described on the <a href="configuration.html">Configuration page</a>.
  454. </p>
  455. </section>
  456. <section id="hints">
  457. <title>Hints</title>
  458. <section id="object-reuse">
  459. <title>Object reuse</title>
  460. <p>
  461. Fop instances shouldn't (and can't) be reused. Please recreate
  462. Fop and FOUserAgent instances for each rendering run using the FopFactory.
  463. This is a cheap operation as all reusable information is held in the
  464. FopFactory. That's why it's so important to reuse the FopFactory instance.
  465. </p>
  466. </section>
  467. <section id="awt">
  468. <title>AWT issues</title>
  469. <p>
  470. If your XSL-FO files contain SVG then Apache Batik will be used. When Batik is
  471. initialised it uses certain classes in <code>java.awt</code> that
  472. intialise the Java AWT classes. This means that a daemon thread
  473. is created by the JVM and on Unix it will need to connect to a
  474. DISPLAY.
  475. </p>
  476. <p>
  477. The thread means that the Java application may not automatically quit
  478. when finished, you will need to call <code>System.exit()</code>. These
  479. issues should be fixed in the JDK 1.4.
  480. </p>
  481. <p>
  482. If you run into trouble running FOP on a head-less server, please see the
  483. <a href="graphics.html#batik">notes on Batik</a>.
  484. </p>
  485. </section>
  486. <section id="render-info">
  487. <title>Getting information on the rendering process</title>
  488. <p>
  489. To get the number of pages that were rendered by FOP you can call
  490. <code>Fop.getResults()</code>. This returns a <code>FormattingResults</code> object
  491. where you can look up the number of pages produced. It also gives you the
  492. page-sequences that were produced along with their id attribute and their
  493. numbers of pages. This is particularly useful if you render multiple
  494. documents (each enclosed by a page-sequence) and have to know the number of
  495. pages of each document.
  496. </p>
  497. </section>
  498. </section>
  499. <section id="performance">
  500. <title>Improving performance</title>
  501. <p>
  502. There are several options to consider:
  503. </p>
  504. <ul>
  505. <li>
  506. Whenever possible, try to use SAX to couple the individual components involved
  507. (parser, XSL transformer, SQL datasource etc.).
  508. </li>
  509. <li>
  510. Depending on the target OutputStream (in case of a FileOutputStream, but not
  511. for a ByteArrayOutputStream, for example) it may improve performance considerably
  512. if you buffer the OutputStream using a BufferedOutputStream:
  513. <code>out = new java.io.BufferedOutputStream(out);</code>
  514. <br/>
  515. Make sure you properly close the OutputStream when FOP is finished.
  516. </li>
  517. <li>
  518. Cache the stylesheet. If you use the same stylesheet multiple times
  519. you can set up a JAXP <code>Templates</code> object and reuse it each time you do
  520. the XSL transformation. (More information can be found
  521. <a class="fork" href="http://www.javaworld.com/javaworld/jw-05-2003/jw-0502-xsl.html">here</a>.)
  522. </li>
  523. <li>
  524. Use an XSLT compiler like <a class="fork" href="http://xml.apache.org/xalan-j/xsltc_usage.html">XSLTC</a>
  525. that comes with Xalan-J.
  526. </li>
  527. <li>
  528. Fine-tune your stylesheet to make the XSLT process more efficient and to create XSL-FO that can
  529. be processed by FOP more efficiently. Less is more: Try to make use of property inheritance where possible.
  530. </li>
  531. </ul>
  532. </section>
  533. <section id="multithreading">
  534. <title>Multithreading FOP</title>
  535. <p>
  536. Apache FOP may currently not be completely thread safe.
  537. The code has not been fully tested for multi-threading issues, yet.
  538. If you encounter any suspicious behaviour, please notify us.
  539. </p>
  540. <p>
  541. There is also a known issue with fonts being jumbled between threads when using
  542. the Java2D/AWT renderer (which is used by the -awt and -print output options).
  543. In general, you cannot safely run multiple threads through the AWT renderer.
  544. </p>
  545. </section>
  546. <section id="examples">
  547. <title>Examples</title>
  548. <p>
  549. The directory "{fop-dir}/examples/embedding" contains several working examples.
  550. </p>
  551. <section id="ExampleFO2PDF">
  552. <title>ExampleFO2PDF.java</title>
  553. <p>This
  554. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleFO2PDF.java?view=markup">
  555. example</a>
  556. demonstrates the basic usage pattern to transform an XSL-FO
  557. file to PDF using FOP.
  558. </p>
  559. <figure src="images/EmbeddingExampleFO2PDF.png" alt="Example XSL-FO to PDF"/>
  560. </section>
  561. <section id="ExampleXML2FO">
  562. <title>ExampleXML2FO.java</title>
  563. <p>This
  564. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2FO.java?view=markup">
  565. example</a>
  566. has nothing to do with FOP. It is there to show you how an XML
  567. file can be converted to XSL-FO using XSLT. The JAXP API is used to do the
  568. transformation. Make sure you've got a JAXP-compliant XSLT processor in your
  569. classpath (ex. <a href="http://xml.apache.org/xalan-j">Xalan</a>).
  570. </p>
  571. <figure src="images/EmbeddingExampleXML2FO.png" alt="Example XML to XSL-FO"/>
  572. </section>
  573. <section id="ExampleXML2PDF">
  574. <title>ExampleXML2PDF.java</title>
  575. <p>This
  576. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup">
  577. example</a>
  578. demonstrates how you can convert an arbitrary XML file to PDF
  579. using XSLT and XSL-FO/FOP. It is a combination of the first two examples
  580. above. The example uses JAXP to transform the XML file to XSL-FO and FOP to
  581. transform the XSL-FO to PDF.
  582. </p>
  583. <figure src="images/EmbeddingExampleXML2PDF.png" alt="Example XML to PDF (via XSL-FO)"/>
  584. <p>
  585. The output (XSL-FO) from the XSL transformation is piped through to FOP using
  586. SAX events. This is the most efficient way to do this because the
  587. intermediate result doesn't have to be saved somewhere. Often, novice users
  588. save the intermediate result in a file, a byte array or a DOM tree. We
  589. strongly discourage you to do this if it isn't absolutely necessary. The
  590. performance is significantly higher with SAX.
  591. </p>
  592. </section>
  593. <section id="ExampleObj2XML">
  594. <title>ExampleObj2XML.java</title>
  595. <p>This
  596. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleObj2XML.java?view=markup">
  597. example</a>
  598. is a preparatory example for the next one. It's an example that
  599. shows how an arbitrary Java object can be converted to XML. It's an often
  600. needed task to do this. Often people create a DOM tree from a Java object and
  601. use that. This is pretty straightforward. The example here, however, shows how
  602. to do this using SAX, which will probably be faster and not even more
  603. complicated once you know how this works.
  604. </p>
  605. <figure src="images/EmbeddingExampleObj2XML.png" alt="Example Java object to XML"/>
  606. <p>
  607. For this example we've created two classes: ProjectTeam and ProjectMember
  608. (found in xml-fop/examples/embedding/java/embedding/model). They represent
  609. the same data structure found in
  610. xml-fop/examples/embedding/xml/xml/projectteam.xml. We want to serialize to XML a
  611. project team with several members which exist as Java objects.
  612. Therefore we created the two classes: ProjectTeamInputSource and
  613. ProjectTeamXMLReader (in the same place as ProjectTeam above).
  614. </p>
  615. <p>
  616. The XMLReader implementation (regard it as a special kind of XML parser) is
  617. responsible for creating SAX events from the Java object. The InputSource
  618. class is only used to hold the ProjectTeam object to be used.
  619. </p>
  620. <p>
  621. Have a look at the source of ExampleObj2XML.java to find out how this is
  622. used. For more detailed information see other resources on JAXP (ex.
  623. <a class="fork" href="http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/xslt/3_generate.html">An older JAXP tutorial</a>).
  624. </p>
  625. </section>
  626. <section id="ExampleObj2PDF">
  627. <title>ExampleObj2PDF.java</title>
  628. <p>This
  629. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleObj2PDF.java?view=markup">
  630. example</a>
  631. combines the previous and the third to demonstrate
  632. how you can transform a Java object to a PDF directly in one smooth run
  633. by generating SAX events from the Java object that get fed to an XSL
  634. transformation. The result of the transformation is then converted to PDF
  635. using FOP as before.
  636. </p>
  637. <figure src="images/EmbeddingExampleObj2PDF.png" alt="Example Java object to PDF (via XML and XSL-FO)"/>
  638. </section>
  639. <section id="ExampleDOM2PDF">
  640. <title>ExampleDOM2PDF.java</title>
  641. <p>This
  642. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleDOM2PDF.java?view=markup">
  643. example</a>
  644. has FOP use a DOMSource instead of a StreamSource in order to
  645. use a DOM tree as input for an XSL transformation.
  646. </p>
  647. </section>
  648. <section id="ExampleSVG2PDF">
  649. <title>ExampleSVG2PDF.java (PDF Transcoder example)</title>
  650. <p>This
  651. <a href="http://svn.apache.org/viewcvs.cgi/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleSVG2PDF.java?view=markup">
  652. example</a>
  653. shows the usage of the PDF Transcoder, a sub-application within FOP.
  654. It is used to generate a PDF document from an SVG file.
  655. </p>
  656. </section>
  657. <section id="example-notes">
  658. <title>Final notes</title>
  659. <p>
  660. These examples should give you an idea of what's possible. It should be easy
  661. to adjust these examples to your needs. Also, if you have other examples that you
  662. think should be added here, please let us know via either the fop-users or fop-dev
  663. mailing lists. Finally, for more help please send your questions to the fop-users
  664. mailing list.
  665. </p>
  666. </section>
  667. </section>
  668. </body>
  669. </document>