3

I have added a DataExporter. If the data grid has Arabic letters, they are not being deployed at all, or they are being displayed disjoint and above each other. Here is an image:

enter image description here

To solve the issue, I have added PDFOption, including a new Arial font.

    pdfOpt = new PDFOptions();
    InputStream fontStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/fonts/arial.ttf");
    BaseFont baseFont = null;
    try {
        baseFont = BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontStream.readAllBytes(), null);
    } catch (IOException ex) {
        Logger.getLogger(PrimefacesPDFExporterOptions.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(PrimefacesPDFExporterOptions.class.getName()).log(Level.SEVERE, null, ex);
    }
    Font font = new Font(baseFont, 12, Font.NORMAL);
    pdfOpt.setFontName(font.getBaseFont().getFullFontName()[0][3]);

The problem with this solution is that I am not being able to set the font it self, just the name of it. So, I need to set the encoding. To do that, I added encoding attribute to the p:dataExporter and set the value to Identity-H:

<p:dataExporter options="#{primefacesPDFExporterOptions.pdfOpt}" encoding="Identity-H" type="pdf" preProcessor="#{primefacesPDFExporterOptions.makePDFLandscape}" pageOnly="false" target="explorerResultsTableId" fileName="data"/>

This did not solve the issue!

Another option I am trying to apply, is to change the font of every cell in the PDF to the created one. But I am not able to get the cells!

1 Answers1

1

This is a basic solution for PF 12, to get the work done, with few changes, but can be improved. I define a custom exporter in the xhtml, like this:

<p:commandButton value="PDF">
    <p:dataExporter type="pdf" target="cars" fileName="cars" exporter="#{dataGridView.exporter}"/>
</p:commandButton>

I use a font compatible with Arabic characters, like ARIALUNI.TTF and I embedded the file in the project (it's easily findable on the internet).
I initialized the exporter on class creation:

public class DataGridView implements Serializable {
    ...
        
    @PostConstruct
    public void init() {
       ...
       exporter = new MyPDFExporter();
    }
    ...
}

MyPDFExporter is most likely PF version (DataTableExporter), I only change the applyFont function, to use the embedded one (I can't get it working directly, passing by option), so it looks like this

protected void applyFont(String fontName, String encoding) {
    InputStream fontStream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/fonts/ARIALUNI.TTF");
    BaseFont baseFont = null;
    try {
        baseFont = BaseFont.createFont("ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontStream.readAllBytes(), null);
    } catch (DocumentException | IOException e) {
    }
    cellFont = new Font(baseFont);
    facetFont = new Font(baseFont);
}

The only change with PF 12 default version is the use of a font compatible with Unicode characters.
AFAIK the older PF version doesn't use openpdf so you need pdfCalligraph to get it works with itext.
Some useful related questions:

WoAiNii
  • 1,003
  • 1
  • 11
  • 19