Sérgio Abreu
Sérgio Abreu

Reputation: 177

Dataexporter pdf header position

I'm using dataexporter to create a pdf of a data table, in my data table the header of the columns is centralized, however the pdf version of the same columns is align to the left. how can I make the columns of the pdf be centralized like the data table.

Upvotes: 1

Views: 4200

Answers (2)

Sérgio Abreu
Sérgio Abreu

Reputation: 177

I use the solution to customize the PDFExporter, it work very well, thank you for your attention. Below is how i've done:

My custom class:

public class CustomPDFExporter extends PDFExporter {

@Override
protected void addColumnFacets(DataTable table, PdfPTable pdfTable, ColumnType columnType) {
    for(UIColumn col : table.getColumns()) {
        if(!col.isRendered()) {
            continue;
        }

        if(col instanceof DynamicColumn) {
            ((DynamicColumn) col).applyModel();
        }

        if(col.isExportable()) {
           addHeaderValue(pdfTable, col.getFacet(columnType.facet()), FontFactory.getFont(FontFactory.TIMES, "iso-8859-1", Font.DEFAULTSIZE, Font.BOLD));
        }
    }
}

protected void addHeaderValue(PdfPTable pdfTable, UIComponent component, Font font) {
   String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component);

   PdfPCell cell = new PdfPCell(new Paragraph(value, font));
   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
   pdfTable.addCell(cell);
}

}

bean:

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

In my page I added:

<h:commandLink action="#{boxBean.exportPDF(boxTable, 'relatorio_caixas')}" >
     <p:graphicImage value="/resources/img/pdf.png"/> 
</h:commandLink>

Upvotes: 2

Tankhenk
Tankhenk

Reputation: 634

Well your answer is already on stackoverflow: changing style on generating pdf with Primefaces dataExporter

Also take a look here: http://www.primefaces.org/showcase/ui/exporterProcessor.jsf how to use the exportProcessor of Primefaces.

In short you need to create your own processor to create an custom PDF

Upvotes: 0

Related Questions