Search code examples
pdfprimefacesdata-export

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.


Solution

  • 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>