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 29KB

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