A
A
Alena2018-01-11 14:19:16
Java
Alena, 2018-01-11 14:19:16

How to export to pdf dataTable (problem with encoding and font)?

Hello,
the problem is in exporting the table to pdf format.

<h:commandLink>
                                    <p:graphicImage value="../../images/pdf.png" width="30"/>
                                    <p:dataExporter type="pdf" target="roadTable" fileName="road" pageOnly="true" encoding="cp1251"/>
                                </h:commandLink>

When specifying windows encoding 1251 - all letters stick together. With UTF-8, Cyrillic is not recognized at all.
I found out that the problem is in the font, because base font in iText HELVETICA - it contains only latin. And how to change the font in the library is not very clear. Found this solution:
Document doc= (Document) document;            
            Font font = FontFactory.getFont("C:\\Windows\\Fonts\\times.ttf", "Cp1251", BaseFont.EMBEDDED);
            BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\times.ttf", "Cp1251", true);
            doc.open();

I insert it into the preprocess - but nothing changes. Either I'm not putting it in there, or I'm not doing it right. Can you please tell me how to export Cyrillic to PDF????

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alena, 2018-01-18
@SAlenaA

It can be useful to someone, I solved this problem like this:
the encoding is "CP1251", there are letters, but all are "stuck together". The problem is in the font. To change the font, you need to rewrite the PDFExporter library class. To do this, we create the org.primefaces.component.export package and create the PDFExporter class in it. We find the source on github and copy it to this file. There is a line in the exportPDFTable method:

private String tahoma="C:\\Windows\\Fonts\\tahoma.ttf";
.........
this.cellFont = FontFactory.getFont(tahoma, string1);
 this.facetFont = FontFactory.getFont(tahoma, string1, Font.DEFAULTSIZE, Font.BOLD);

Instead of tahoma, you can specify any fonts. containing Cyrillic and everything is OK.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question