vandeseer / easytable

Small table drawing library built upon Apache PDFBox
MIT License
239 stars 91 forks source link

[BUG] IllegalArgumentException: U+0443 ('afii10085') is not available in this font #130

Closed Adriansun closed 3 years ago

Adriansun commented 3 years ago

Issue:

Exception in thread "pool-1-thread-1" java.lang.IllegalArgumentException: U+0443 ('afii10085') is not available in this font Helvetica-Bold encoding: WinAnsiEncoding at org.apache.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:427) at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:332) at org.apache.pdfbox.pdmodel.PDPageContentStream.showTextInternal(PDPageContentStream.java:514) at org.apache.pdfbox.pdmodel.PDPageContentStream.showText(PDPageContentStream.java:476) at org.vandeseer.easytable.drawing.DrawingUtil.drawText(DrawingUtil.java:20) at org.vandeseer.easytable.drawing.cell.TextCellDrawer.drawText(TextCellDrawer.java:106) at org.vandeseer.easytable.drawing.cell.TextCellDrawer.drawContent(TextCellDrawer.java:57) at org.vandeseer.easytable.TableDrawer.lambda$new$0(TableDrawer.java:69) at org.vandeseer.easytable.TableDrawer.drawRow(TableDrawer.java:200) at org.vandeseer.easytable.TableDrawer.drawWithFunction(TableDrawer.java:183) at org.vandeseer.easytable.TableDrawer.lambda$drawPage$1(TableDrawer.java:91) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.vandeseer.easytable.TableDrawer.drawPage(TableDrawer.java:90) at org.vandeseer.easytable.TableDrawer.draw(TableDrawer.java:157) at application.utils.pdf.PdfCreationUtils.createAndSaveDocumentWithTables(PdfCreationUtils.java:115) at application.utils.pdf.PdfCreationUtils.createAndSaveDocumentWithTables(PdfCreationUtils.java:87) at application.user.service.pdf.factory.user.UserPdfFactoryImpl.createUserPdf(UserPdfFactoryImpl.java:89) at application.user.queue.pdf.user.UserPdfQueueImpl.lambda$initiatePdfThreads$0(UserPdfQueueImpl.java:64) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) at java.base/java.lang.Thread.run(Thread.java:831)

This is from the " .draw(() -> document, () -> new PDPage(PDRectangle.A4), 0);" line below:

final PDPage page = new PDPage(PDRectangle.A4); document.addPage(page);

final float pageHeight = getPageHeight(page).orElse(0f);
float startY = pageHeight;

try (final PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
  Table table;

  for (int i = 0; i < tables.length; i++) {
    if (i != 0 && i != 1 && i != 2 && i != 3) {
      table = colorFillRestOfPage(tables[i], pageHeight);
    } else {
      table = tables[i].build();
    }

    TableDrawer.builder()
        .page(page)
        .contentStream(contentStream)
        .table(table)
        .startX(1f)
        .startY(startY)
        .build()
        .draw(() -> document, () -> new PDPage(PDRectangle.A4), 0);

    startY -= (table.getHeight());
  }
}

The above code is a part of my create PDF method. Am I doing something wrong or is this a easyTable issue?

Adriansun commented 3 years ago

Found some information on it: https://stackoverflow.com/questions/46644570/pdfbox-u000a-controllf-is-not-available-in-this-font-helvetica-encoding

Adriansun commented 3 years ago

This seems to only happen with "non-english" characters, like korean, chinese, or similar for me. Some clue to what is happening: https://stackoverflow.com/questions/6334134/fpdf-utf-8-encoding-how-to

Adriansun commented 3 years ago

I found the problem. It is an encoding issue. When I create a PDF for a user and try to print out non-english characters, like "日本語" (Japanese), then I get a crash. Encoding of the characters seems to be the problem. The user PDFs are like the EU GDPR - you get to know everything about you that the server has on you. Whenever a special character is used, then easyTable crash. I would like to assume that UTF-8 is already being used, and UTF-8 does support asian/russian/greek characters (right?), so I do not see where the problem is in a way. Would like to see a speedy resolution. Thanks for a, otherwise, great PDF creator.

Adriansun commented 3 years ago

Hmm maybe this can help somewhat: https://stackoverflow.com/questions/51481600/handle-many-unicode-caracters-with-pdfbox

vandeseer commented 3 years ago

This is not a bug of easytable. I think there is already another closed issue from another user having the same problem: https://github.com/vandeseer/easytable/issues/38

Adriansun commented 3 years ago

Thank you for the reply. Can we talk a bit more about this, please?

I have looked through your links and others issues that are similar to the font problem. I do not know what to say. Are we saying that we can only make PDFs with 'English' characters and people in Asia cannot make any PDFs at all? It does look in one link you provided that one can load some other font that might have the support for non English characters. I believe we need to work together with this problem. Kind of scary that we cannot use eg Greek characters. Any idea of what we can do?

Adriansun commented 3 years ago

Also are you saying that this bug is stemming from Apache PDFBox (https://pdfbox.apache.org/ )? If so; have you talked with them directly about this? I can support your in this if you like. I believe it would leave a bigger mark if you would create a issue at their place and then others join in as support.