11
votes

I have an editable <p:dataTable> with <p:cellEditor> and I want to export the content of that table into PDF format using <p:dataExporter>.

I have included the itext 2.1.7 jar. I got the output in PDF but it shows the Object#toString() values of all <p:cellEditor> components like so:

org.primefaces.component.celleditor.CellEditor@1bd59e1

How do I export the output values of the <p:cellEditor> instead?

2
Without showing some code, I'm afraid it's going to be hard for anyone to provide any help.Arjan Tijms

2 Answers

12
votes

The <p:cellEditor> is indeed not recognized by the PrimeFaces standard data exporters. I've previously reported this to the PF guys as issue 4013 with an example which not only mentions CellEditor, but also HtmlGraphicImage (we are using images to show boolean states, whose alt we'd like to show in PDF/XML/XLS/CSV reports).

First, create a new class which extends the standard PDFExporter like follows:

public class ExtendedPDFExporter extends PDFExporter {

    @Override
    protected String exportValue(FacesContext context, UIComponent component) {
        if (component instanceof CellEditor) {
            return exportValue(context, ((CellEditor) component).getFacet("output"));
        }
        else if (component instanceof HtmlGraphicImage) {
            return (String) component.getAttributes().get("alt");
        }
        else {
            return super.exportValue(context, component);
        }
    }

}

Then, to use it, call it programmatically instead of via <p:dataExporter>.

<p:dataTable binding="#{table}" editable="true" ...>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename')}" />

With

public void exportPDF(DataTable table, String filename) throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    Exporter exporter = new ExtendedPDFExporter();
    exporter.export(context, table, filename, false, false, "UTF-8", null, null);
    context.responseComplete();
}

Feel free to find the data table by UIComponent#findComponent() instead and to set the filename in action method only. The above code is just exemplary.

4
votes

I agree, I also find this approach to customize the Exporter behaviour the most flexible and least painful.

Anyone interested in using the preProcessor/postProcessor methods with this? Here's an example how to do that.

I dared to slightly modify the method from the answer above:

public void exportPDF(DataTable table, String filename, 
        String preProcessor, String postProcessor) throws IOException {

    FacesContext context = FacesContext.getCurrentInstance();
    ExpressionFactory factory = context.getApplication().getExpressionFactory();

    MethodExpression preProcessorME = factory.createMethodExpression(
        context.getELContext(), preProcessor, null, new Class[] {Object.class});
    MethodExpression postProcessorME = factory.createMethodExpression(
        context.getELContext(), postProcessor, null, new Class[] {Object.class});

    Exporter exporter = new ExtendedPDFExporter();
    exporter.export(context, table, filename, false, false, "UTF-8", 
        preProcessorMe, postProcessorME);

    context.responseComplete();

}

And this is how you use it in your page (again, I just modified the above example):

<p:dataTable binding="#{table}" editable="true" ...>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column><p:cellEditor>...</p:cellEditor></p:column>
    <p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename', 
    '#{yourBean.preProcessPDF}', '#{yourBean.postProcessPDF}')}" />

Notice that there ARE NO NESTED EL STATEMENTS (that is not allowed anyway), the last two arguments are simple Strings containing EL expressions.