This page discusses topic all around using Apache FOP in a servlet environment.
In the directory {fop-dir}/src/java/org/apache/fop/servlet, you'll find a working example of a FOP-enabled servlet.
The servlet is automatically built when you build Apache FOP using the supplied Ant script. After building the servlet, drop fop.war into the webapps directory of Apache Tomcat (or any other web container). Then, you can use URLs like the following to generate PDF files:
The source code for the servlet can be found under {fop-dir}/src/java/org/apache/fop/servlet/FopServlet.java.
Here is a minimal code snippet to demonstrate the basics:
A common requirement is to transform an XML source to XSL-FO using an XSL transformation. It is recommended to use JAXP for this task. The following snippet shows the basic code:
The Source
instance used above is simply an
example. If you have to read the XML from a string, supply
a new StreamSource(new
StringReader(xmlstring))
. Constructing and reparsing
an XML string is generally less desirable than using a
SAXSource if you generate your XML. You can alternatively
supply a DOMSource as well. You may also use dynamically
generated XSL if you like.
Because you have an explicit Transformer
object, you can also use it to
explicitely set parameters for the transformation run.
You can easily set up your own FOUserAgent as demonstrated on the Embedding page.
There are several options to consider:
org.apache.commons.io.output.ByteArrayOutputStream
Of course, the performance hints from the Embedding page apply here, too.
Often, you will want to use resources (stylesheets, images etc.) which are bundled with
your web application. FOP provides a URIResolver implementation that lets you access
files via the Servlet's ServletContext. The class is called
org.apache.fop.servlet.ServletContextURIResolver
.
Here's how to set it up in your servlet. Instantiate a new instance in the servlet's init() method:
The ServletContextURIResolver reacts on URIs beginning with "servlet-context:". If you want to access an image in a subdirectory of your web application, you could, for example, use: "servlet-context:/images/myimage.png". Don't forget the leading slash after the colon!
Further down, you can use the URIResolver for various things:
Here are some example snippets:
Some versions of Internet Explorer will not automatically show the PDF or call the servlet multiple times. These are well-known limitations of Internet Explorer and are not a problem of the servlet. However, Internet Explorer can still be used to download the PDF so that it can be viewed later. Here are some suggestions in this context:
.pdf
, like
http://myserver/servlet/stuff.pdf
. Yes, the servlet can
be configured to handle this. If the URL has to contain parameters,
try to have both the base URL as well as the last parameter end in
.pdf
, if necessary append a dummy parameter, like
http://myserver/servlet/stuff.pdf?par1=a&par2=b&d=.pdf
. The
effect may depend on IEx version.
Expires
header entry may help in
this case:response.setDateHeader("Expires",
System.currentTimeMillis() + cacheExpiringDuration *
1000);
When using a servlet engine, there are potential CLASSPATH issues, and potential conflicts with existing XML/XSLT libraries. Servlet containers also often use their own classloaders for loading webapps, which can cause bugs and security problems.
Check Tomcat's documentation for detailed instructions about installing FOP and Cocoon. There are known bugs that must be addressed, particularly for Tomcat 4.0.3.
Put a copy of a working parser in some directory where WebSphere can access it. For example, if /usr/webapps/yourapp/servlets is the CLASSPATH for your servlets, copy the Xerces jar into it (any other directory would also be fine). Do not add the jar to the servlet CLASSPATH, but add it to the CLASSPATH of the application server which contains your web application. In the WebSphere administration console, click on the "environment" button in the "general" tab. In the "variable name" box, enter "CLASSPATH". In the "value" box, enter the correct path to the parser jar file (/usr/webapps/yourapp/servlets/Xerces.jar in our example here). Press "OK", then apply the change and restart the application server.
Sometimes the requirements for a servlet get quite sophisticated: SQL data sources, multiple XSL transformations, merging of several datasources etc. In such a case consider using Apache Cocoon instead of a custom servlet to accomplish your goal.