vandeseer / easytable

Small table drawing library built upon Apache PDFBox
MIT License
246 stars 94 forks source link

Exception when adding cyrillic text #31

Closed bangieff closed 5 years ago

bangieff commented 5 years ago

I tried creating a table with cyrillic text in it, but I get an exception in TableDrawer->draw(); The same snippet works fine with text in English.

    final Row headerRow = Row.builder().add(CellText.builder().text("Проба").build()).build();
    tableBuilder.addRow(headerRow);
    float startY = page.getMediaBox().getHeight() - 10;
    TableDrawer.builder().contentStream(contentStream).table(tableBuilder.build())
            .startY(startY).build()
            .draw();
vandeseer commented 5 years ago

You will need to load a custom font for cyrilic characters. The default font that is used for tables in easytable is PDType1Font.HELVETICA and it doesn't support them. So you will also experience the same issue if you just use PDFbox and create a PDF with the text you mentioned:

final PDPageContentStream contentStream = new PDPageContentStream(document, page);
final String text = "Проба";
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.newLineAtOffset(50, page.getMediaBox().getHeight() - 50);
contentStream.showText(text);
contentStream.endText();
contentStream.close();

You would need to load a font that supports it and then pass it as an argument to the table builder: tableBuilder.font(<YOUR_FONT>).

For more information have a look here:

So basically this is not an easytable issue, so I will close it ;)