Custom exporting p:dataTable data from .xls to .pdf

AdrianES Source

I am from Macedonia, and in my country we use cyrillic font.

I am developing a web application in NetBeans IDE and using PrimeFaces 5.2 framework. For exporting I use poi-3.9.jar and itext-2.7.1.jar. I created a table <p:dataTable> that I fill with some of the columns having cyrillic text (columns like name, surname etc.). In my application I like to use export to PDF and export to Excel so client can export data from the table as a report or something else.

Exporting to Excel works great. I can see all the data (including cyrillic text) and can modify it in postProcessor. In postProcessor I make the header of my table with blue background, add border to every cell, make rows bigger if they have multiple lines, autosize all columns etc.

Code from the xhtml:

    <p:dataTable var="u" value="#{WebClient.allRemoteParkingUsers}" id="uncheckedParkingRequests"
                             rowKey="#{u.ID}" editable="true" tableStyle="table-layout: auto;"
                             emptyMessage="Не постои ниеден паркинг клиент според податоците по кои пребарувате."
                             filteredValue="#{WebClient.filtered_users}" widgetVar="client_table"
                             paginator="true" rows ="20" rowsPerPageTemplate="20,50,100" paginatorAlwaysVisible="true" 
                             paginatorPosition="bottom" paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown} {Exporters}">
                    <f:facet name="header" id="naslovTabela">
                        <p:outputLabel value="Листа на паркинг клиенти"/>
                        <p:outputPanel style="text-align: right; font-size: 18px;">
                            <h:outputText value="Глобално пребарување:" style="margin-right: 5px;"/>
                            <p:inputText id="globalFilter" onkeyup="PF('client_table').filter()" style="width:200px" placeholder="внесете клучен збор" />
                        </p:outputPanel>
                    </f:facet>
                    <f:facet name="{Exporters}">
                        <h:commandLink style="color: white; font-size: 15px; text-align: center">
                            <h:outputText value="Сними во Excel" />
                            <img src="images/excel_icon.png" alt=""/>
                            <p:dataExporter type="xls" target="uncheckedParkingRequests" fileName="parking_user_excel" postProcessor="#{doc.postProcessXLS}" />
                        </h:commandLink>
                        <h:commandLink style="color: white; font-size: 15px; text-align: center">
                            <h:outputText value="Сними во PDF" />
                            <img src="images/pdf_icon.png" alt=""/>
                            <p:dataExporter type="pdf" target="uncheckedParkingRequests" fileName="file" encoding="UTF-8" />
                        </h:commandLink>
                    </f:facet>
...
</p:dataTable>

DocumentBuilder class:

    import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.HSSFColor;
//POI libraries to read Excel File
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.util.Iterator;
//itext libraries to write PDF file

/**
 *
 * @author adrian
 */
@ManagedBean(name = "doc", eager = true)
public class DocumentBuilder implements Serializable {

    public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        //Aply style for all cells
        Iterator rowIter = sheet.rowIterator();
        boolean first_row = true;
        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            int row_max_number_of_breaks = 0;
            while (cellIter.hasNext()) {
                HSSFCell cell = (HSSFCell) cellIter.next();
                HSSFCellStyle cell_style = wb.createCellStyle();
                if (first_row) { //Header cell
                    cell_style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
                    cell_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                }
                cell_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                cell_style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
                cell_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                cell_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
                cell_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
                cell_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                cell_style.setWrapText(true);
                cell.setCellStyle(cell_style);
                String cell_value = cell.getStringCellValue();
                int cell_number_of_break = 0;
                while (cell_value.contains("<br/>")) {
                    cell_value = cell_value.replaceFirst("<br/>", "\n");
                    cell.setCellValue(cell_value);
                    cell_number_of_break += 1;
                }
                if (row_max_number_of_breaks < cell_number_of_break) {
                    row_max_number_of_breaks = cell_number_of_break;
                }
            }
            myRow.setHeight(Short.valueOf(String.valueOf((myRow.getHeight() * (row_max_number_of_breaks + 1)))));
            first_row = false;
        }
        //Resize columns to fit data
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
    }

}

When I try exporting the same table into PDF with the PrimeFaces exporter every field that has cyrillic text is not visible. When I export from my Excel file into .pdf everything is fine (I can see the cyrillic text plus I got the design from the DocumentBuilder class as extra).

I have researched and already tried changing the encoding to UTF-8, CP1252, Windows-1252 etc., but still nothing.

Is it possible to make a custom exporter that first will create the Excel document, export it in PDF and send it (with some magic code in postProcessor or preProcessor :) )

So I can keep the design from the Excel and working export to PDF.

excelpdfprimefacesexportcyrillic

Answers

comments powered by Disqus