Package org.apache.fop.servlet
Class FopServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- org.apache.fop.servlet.FopServlet
-
- All Implemented Interfaces:
java.io.Serializable
,javax.servlet.Servlet
,javax.servlet.ServletConfig
- Direct Known Subclasses:
FopPrintServlet
public class FopServlet extends javax.servlet.http.HttpServlet
Example servlet to generate a PDF from a servlet.
Servlet param is:- fo: the path to a XSL-FO file to render
- xml: the path to an XML file to render
- xslt: the path to an XSLT file that can transform the above XML to XSL-FO
Example URL: http://servername/fop/servlet/FopServlet?fo=readme.fo
Example URL: http://servername/fop/servlet/FopServlet?xml=data.xml
For this to work with Internet Explorer, you might need to append "ext=.pdf" to the URL. (todo) Ev. add caching mechanism for Templates objects- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.String
FO_REQUEST_PARAM
Name of the parameter used for the XSL-FO fileprotected FopFactory
fopFactory
The FopFactory used to create Fop instancesprivate static long
serialVersionUID
protected javax.xml.transform.TransformerFactory
transFactory
The TransformerFactory used to create Transformer instancesprotected javax.xml.transform.URIResolver
uriResolver
URIResolver for use by this servletprotected static java.lang.String
XML_REQUEST_PARAM
Name of the parameter used for the XML fileprotected static java.lang.String
XSLT_REQUEST_PARAM
Name of the parameter used for the XSLT file
-
Constructor Summary
Constructors Constructor Description FopServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
configureFopFactory(FopFactoryBuilder builder)
This method is called right after the FopFactory is instantiated and can be overridden by subclasses to perform additional configuration.protected javax.xml.transform.Source
convertString2Source(java.lang.String param)
Converts a String parameter to a JAXP Source object.void
doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected FOUserAgent
getFOUserAgent()
void
init()
protected void
render(javax.xml.transform.Source src, javax.xml.transform.Transformer transformer, javax.servlet.http.HttpServletResponse response)
Renders an input file (XML or XSL-FO) into a PDF file.protected void
renderFO(java.lang.String fo, javax.servlet.http.HttpServletResponse response)
Renders an XSL-FO file into a PDF file.protected void
renderXML(java.lang.String xml, java.lang.String xslt, javax.servlet.http.HttpServletResponse response)
Renders an XML file into a PDF file by applying a stylesheet that converts the XML to XSL-FO.private void
sendPDF(byte[] content, javax.servlet.http.HttpServletResponse response)
-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
FO_REQUEST_PARAM
protected static final java.lang.String FO_REQUEST_PARAM
Name of the parameter used for the XSL-FO file- See Also:
- Constant Field Values
-
XML_REQUEST_PARAM
protected static final java.lang.String XML_REQUEST_PARAM
Name of the parameter used for the XML file- See Also:
- Constant Field Values
-
XSLT_REQUEST_PARAM
protected static final java.lang.String XSLT_REQUEST_PARAM
Name of the parameter used for the XSLT file- See Also:
- Constant Field Values
-
transFactory
protected javax.xml.transform.TransformerFactory transFactory
The TransformerFactory used to create Transformer instances
-
fopFactory
protected FopFactory fopFactory
The FopFactory used to create Fop instances
-
uriResolver
protected transient javax.xml.transform.URIResolver uriResolver
URIResolver for use by this servlet
-
-
Method Detail
-
init
public void init() throws javax.servlet.ServletException
- Overrides:
init
in classjavax.servlet.GenericServlet
- Throws:
javax.servlet.ServletException
-
configureFopFactory
protected void configureFopFactory(FopFactoryBuilder builder)
This method is called right after the FopFactory is instantiated and can be overridden by subclasses to perform additional configuration.
-
doGet
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException
- Overrides:
doGet
in classjavax.servlet.http.HttpServlet
- Throws:
javax.servlet.ServletException
-
convertString2Source
protected javax.xml.transform.Source convertString2Source(java.lang.String param)
Converts a String parameter to a JAXP Source object.- Parameters:
param
- a String parameter- Returns:
- Source the generated Source object
-
sendPDF
private void sendPDF(byte[] content, javax.servlet.http.HttpServletResponse response) throws java.io.IOException
- Throws:
java.io.IOException
-
renderFO
protected void renderFO(java.lang.String fo, javax.servlet.http.HttpServletResponse response) throws FOPException, javax.xml.transform.TransformerException, java.io.IOException
Renders an XSL-FO file into a PDF file. The PDF is written to a byte array that is returned as the method's result.- Parameters:
fo
- the XSL-FO fileresponse
- HTTP response object- Throws:
FOPException
- If an error occurs during the rendering of the XSL-FOjavax.xml.transform.TransformerException
- If an error occurs while parsing the input filejava.io.IOException
- In case of an I/O problem
-
renderXML
protected void renderXML(java.lang.String xml, java.lang.String xslt, javax.servlet.http.HttpServletResponse response) throws FOPException, javax.xml.transform.TransformerException, java.io.IOException
Renders an XML file into a PDF file by applying a stylesheet that converts the XML to XSL-FO. The PDF is written to a byte array that is returned as the method's result.- Parameters:
xml
- the XML filexslt
- the XSLT fileresponse
- HTTP response object- Throws:
FOPException
- If an error occurs during the rendering of the XSL-FOjavax.xml.transform.TransformerException
- If an error occurs during XSL transformationjava.io.IOException
- In case of an I/O problem
-
render
protected void render(javax.xml.transform.Source src, javax.xml.transform.Transformer transformer, javax.servlet.http.HttpServletResponse response) throws FOPException, javax.xml.transform.TransformerException, java.io.IOException
Renders an input file (XML or XSL-FO) into a PDF file. It uses the JAXP transformer given to optionally transform the input document to XSL-FO. The transformer may be an identity transformer in which case the input must already be XSL-FO. The PDF is written to a byte array that is returned as the method's result.- Parameters:
src
- Input XML or XSL-FOtransformer
- Transformer to use for optional transformationresponse
- HTTP response object- Throws:
FOPException
- If an error occurs during the rendering of the XSL-FOjavax.xml.transform.TransformerException
- If an error occurs during XSL transformationjava.io.IOException
- In case of an I/O problem
-
getFOUserAgent
protected FOUserAgent getFOUserAgent()
- Returns:
- a new FOUserAgent for FOP
-
-