I am trying to convert my ADF table in J-Developer v.11.1.2.4.0 to a PDF so users can click a button and have a printable/savable PDF.
I have been following the Waslley Souza Blog with a few modifications.
http://waslleysouza.com.br/en/2015/03/export-table-as-a-pdf-file-in-adf/
The previous blog on making a XML file works very well, however the second on PDF is causing hours of frustration
as my XML skills are being developed.
So on my .jsf page I have a button with the following:
<af:commandButton text="Export To PDF" id="cb3" inlineStyle="border-width:inherit;">
<af:fileDownloadActionListener filename="Show.pdf"
method="#{pageFlowScope.ShowTFBean.exportAllRowsToPDF}"
contentType="application/pdf"/>
</af:commandButton>
My Java bean is as follows which compiles and produces no errors:
private static String
TEMPLATE_PDF_SHOW = "styleSheetsPDF/show.xsl";
private static TransformerFactory tFactory = new TransformerFactoryImpl();
private static FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
...Constructor not listed
private InputStream getTemplate(String template)
{
FacesContext fc = FacesContext.getCurrentInstance();
return fc.getExternalContext().getResourceAsStream(template);
}
private void convertDOM2PDF(Node xml, InputStream template, OutputStream out) throws TransformerConfigurationException, FOPException, TransformerException
{
try
{ //Configure foUserAgnet as desired FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
try
{ //Construct fop with desired output format and output stream
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup Idenity transformer
Transformer transformer = tFactory.newTransformer(new StreamSource(template));
// Setup input for XSLT transformer
Source src = new DOMSource(xml);
//Resulting SAX events (the generated fo) must be piped through to
FOP Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing transformer.transform(src, res);
} finally
{ out.close();
}
} catch(Exception e) { e.printStackTrace(System.err);
}
}
public void exportSelectedRowsToPDF(FacesContext fc, OutputStream os) throws TransformerConfigurationException, FOPException, TransformerException
{
RichTable table = (RichTable)JSFUtils.findComponentInRoot("t1");
RowKeySet rsk = table.getSelectedRowKeys();
Iterator iter = rsk.iterator();
StringBuilder id = new StringBuilder("");
while(iter.hasNext())
{
Key key = (Key)((List)iter.next()).get(0);
id.append(key.getKeyValues()[0]);
if(iter.hasNext())
{
id.append(",");
}
}
DCIteratorBinding dc = ADFUtils.findIterator("ShowIterator");
ViewObject vo = dc.getViewObject();
RowSet rs = ((ShowImpl)vo).filterById(id.toString());
convertDOM2PDF(rs.writeXML(0, XMLInterface.XML_OPT_ALL_ROWS), getTemplate(TEMPLATE_PDF_SHOW), os);
}
public void exportAllRowsToPDF(FacesContext fc, OutputStream os) throws TransformerConfigurationException, FOPException, TransformerException
{
DCIteratorBinding dc = ADFUtils.findIterator("ShowIterator");
ViewObject vo = dc.getViewObject();
convertDOM2PDF(vo.writeXML(0, XMLInterface.XML_OPT_ALL_ROWS), getTemplate(TEMPLATE_PDF_SHOW), os);
}
I believe the issues is in the .xls file below as the error I receive is
org.apache.fop.fo.validationexception: Document is empty (something might be wrong with your XSLT stylesheet)
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
<!--=========================-->
<!--root element: Employees-->
<!--=========================-->
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master
master-name="simpleA4"
page-height="29.7cm"
page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="16pt" font-weight="bold" space-after="5mm">Employees</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-header text-align="center" background-color="silver" font-weight="bold">
<fo:table-row> <fo:table-cell padding="1mm" border-style="solid">
<fo:block>Name</fo:block>
</fo:table-cell> <fo:table-cell padding="1mm" border-style="solid">
<fo:block>Project</fo:block>
</fo:table-cell> <fo:table-cell padding="1mm" border-style="solid">
<fo:block>Dog Name</fo:block>
</fo:table-cell> <fo:table-cell padding="1mm" border-style="solid">
<fo:block>Task Name</fo:block>
</fo:table-cell> <fo:table-cell padding="1mm" border-style="solid">
<fo:block>Employee</fo:block>
</fo:table-cell> </fo:table-row>
</fo:table-header> <fo:table-body>
<xsl:apply-templates select="Employee"/> </fo:table-body> </fo:table> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template>
<!--=========================--> <!--child element: Employee--> <!--=========================-->
<xsl:template match="/"> <fo:table-row> <fo:table-cell> <fo:block>
<xsl:value-of select="Name"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block>
<xsl:value-of select="Description"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block>
<xsl:value-of select="ShortName"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block>
<xsl:value-of select="TaskName"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block>
<xsl:value-of select="Employee"/> </fo:block> </fo:table-cell> </fo:table-row> </xsl:template> </xsl:stylesheet>
When I run the application the page displays, I click the button and then error listed above is produced. Any help will be highly prized.