I have a Primefaces DataGrid that I export whith Primefaces DataExporter but I can't figure out how to resize the columns.
I added a preporcessor
<p:dataExporter type="pdf" target="tbl" fileName="cars" preProcessor="#{customizedDocumentsView.preProcessPDF}" />
And this is the code in my bean
public void preProcessPDF(Object document) {
Document pdf = (Document) document;
pdf.open();
pdf.setPageSize(PageSize.A4);
//I need to do something like that
//PdfPTable table = new PdfPTable(4);
//float[] columnWidths = new float[] {10f, 20f, 30f, 10f};
//table.setWidths(columnWidths);
}
Is there any way to do this ?
Recently I inherited project with many already existing exporters with already set options tags so I needed to find solution that will keep all of them intact.
I got an idea from this accepted answer.
My solution sets column widths via options tag of p:dataExporter so there is no need for pre/post processing. I tested it using Primefaces ver 4.x and up.
Step by step procedure:
Create new package
org.primefaces.component.exportinside your project.From Primefaces git repository fully copy following classes ExporterOptions ,PDFOptions, Exportedfactory and PDFExporter into newly created package.
In ExporterOptions add
public float[] getColumnWidths();In PDFOptions add
float[] columnWidths;and add getter and setter.In ExporterFactory modify line
exporter = new PdfExporter();toexporter = new CustomPdfExporter();Rename PDFExporter class to CustomPDFExporter and keep just following export method fully
Delete code from other export methods but keep declarations.
Inside CustomPDFExporter add 2 lines in ExportPdfTable method
}
Now you are ready to go managed bean and add pdf option. For example
Finally your
p:dataExportercomponent should look like thisThis solution using PF showcase produces following result
Suggestion for extending this solution:
Primefaces exporter uses iText ver 2.1.7. with old but still powerful API. For example, in ExporterOptions in step 1. you can add
to set absolute column widths or you can set any other option driven by your project requirements.