C
couellette
Hello,
I am trying to build a PDF page from an XML and an XSL:FO document from
inside a servlet. My XSL:FO is using <xsl:import> to reference
additional stylesheets for style as well as other support templates.
These support stylesheets are in the same directory as the master
XSL:FO doc.
My parent XSL:FO doc runs fine when I run it through a renderer like
XMLSpy, but when I run the transform from a servlet, my PDF document is
empty. I have traced this problem to my <xsl:import> statement which I
originally had using a relative path. If I change the relative path to
an absolute path, the thing runs perfect. However, using absolute paths
is not an option for me since I need the same code to work on both
Windows and UNIX.
Any suggestions on this? Here is some sample code:
JSP which calls FOP:
<%@ page import = "java.io.*, javax.xml.transform.*,
javax.xml.transform.stream.*, org.xml.sax.InputSource,
org.apache.fop.apps.*, org.apache.fop.messaging.*,
javax.xml.transform.stream.StreamSource" %>
<%
try{
String xmlBase = "/WEB-INF/XML/";
String detailXML = (String)request.getAttribute("DETAIL_XML");
ServletContext context = getServletContext();
String origin = (String)request.getParameter("origin");
String navigation = origin.toUpperCase();
String fileName = "ViewMessages.pdf";
response.setContentType( "application/pdf" );
response.setHeader("Content-disposition", "attachment; filename="
+fileName );
String styleSheet = "ViewPDFDetails";
java.net.URL xslURL = context.getResource(xmlBase + "xsl/" +
styleSheet + ".xslt");
java.io.ByteArrayOutputStream pdfOutput = new
java.io.ByteArrayOutputStream();
java.io.ByteArrayOutputStream interimOut = new
java.io.ByteArrayOutputStream() ;
InputStream xslInputStream = ( InputStream )xslURL.getContent();
StringBufferInputStream xmlInputStream = new StringBufferInputStream(
detailXML );
StreamSource xmlSource = new StreamSource(xmlInputStream);
StreamSource xslSource = new StreamSource(xslInputStream);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult( interimOut ));
String foOutput = interimOut.toString();
Driver driver = new Driver(new InputSource(new StringReader(
foOutput)), pdfOutput);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
ByteArrayInputStream bais = new ByteArrayInputStream(
pdfOutput.toByteArray() );
java.io.BufferedInputStream bis = new java.io.BufferedInputStream(
bais );
int bit = 1024;
try {
while ((bit) >= 0) {
bit = bis.read();
out.write(bit);
}
}catch(Exception e){
}
out.flush();
out.close();
bis.close();
}catch(Exception e){}
XSL:FO
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xalan="http://xml.apache.org/xslt"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookupCache="xalan://bbh.xml.helper.ui.LookupCache"
xmlns:dateHelper="xalan://bbh.mbter.helpers.DateFormat"
xmlns:textHelper="xalan://bbh.mbter.helpers.text.TextConverter"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="./styles.xsl" />
<xsl:template match="/">
<fo:root>
..
..
..
</fo:root>
</xsl:template>
</xsl:transform>
I am trying to build a PDF page from an XML and an XSL:FO document from
inside a servlet. My XSL:FO is using <xsl:import> to reference
additional stylesheets for style as well as other support templates.
These support stylesheets are in the same directory as the master
XSL:FO doc.
My parent XSL:FO doc runs fine when I run it through a renderer like
XMLSpy, but when I run the transform from a servlet, my PDF document is
empty. I have traced this problem to my <xsl:import> statement which I
originally had using a relative path. If I change the relative path to
an absolute path, the thing runs perfect. However, using absolute paths
is not an option for me since I need the same code to work on both
Windows and UNIX.
Any suggestions on this? Here is some sample code:
JSP which calls FOP:
<%@ page import = "java.io.*, javax.xml.transform.*,
javax.xml.transform.stream.*, org.xml.sax.InputSource,
org.apache.fop.apps.*, org.apache.fop.messaging.*,
javax.xml.transform.stream.StreamSource" %>
<%
try{
String xmlBase = "/WEB-INF/XML/";
String detailXML = (String)request.getAttribute("DETAIL_XML");
ServletContext context = getServletContext();
String origin = (String)request.getParameter("origin");
String navigation = origin.toUpperCase();
String fileName = "ViewMessages.pdf";
response.setContentType( "application/pdf" );
response.setHeader("Content-disposition", "attachment; filename="
+fileName );
String styleSheet = "ViewPDFDetails";
java.net.URL xslURL = context.getResource(xmlBase + "xsl/" +
styleSheet + ".xslt");
java.io.ByteArrayOutputStream pdfOutput = new
java.io.ByteArrayOutputStream();
java.io.ByteArrayOutputStream interimOut = new
java.io.ByteArrayOutputStream() ;
InputStream xslInputStream = ( InputStream )xslURL.getContent();
StringBufferInputStream xmlInputStream = new StringBufferInputStream(
detailXML );
StreamSource xmlSource = new StreamSource(xmlInputStream);
StreamSource xslSource = new StreamSource(xslInputStream);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult( interimOut ));
String foOutput = interimOut.toString();
Driver driver = new Driver(new InputSource(new StringReader(
foOutput)), pdfOutput);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
ByteArrayInputStream bais = new ByteArrayInputStream(
pdfOutput.toByteArray() );
java.io.BufferedInputStream bis = new java.io.BufferedInputStream(
bais );
int bit = 1024;
try {
while ((bit) >= 0) {
bit = bis.read();
out.write(bit);
}
}catch(Exception e){
}
out.flush();
out.close();
bis.close();
}catch(Exception e){}
XSL:FO
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xalan="http://xml.apache.org/xslt"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookupCache="xalan://bbh.xml.helper.ui.LookupCache"
xmlns:dateHelper="xalan://bbh.mbter.helpers.DateFormat"
xmlns:textHelper="xalan://bbh.mbter.helpers.text.TextConverter"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="./styles.xsl" />
<xsl:template match="/">
<fo:root>
..
..
..
</fo:root>
</xsl:template>
</xsl:transform>