Search code examples
javaunicodeprintingpdfboxtruetype

PDFBox with ttf font - No Unicode mapping


I'm trying to create a PDF document using Apache PDFBox 2.0.18, then print it.

Everything works fine, but when printing I receive a lot of warning events:

Jun 18, 2020 5:24:05 PM org.apache.pdfbox.pdmodel.font.PDType0Font toUnicode
WARNING: No Unicode mapping for CID+73 (73) in font ArialMT
Jun 18, 2020 5:24:05 PM org.apache.pdfbox.pdmodel.font.PDCIDFontType2 codeToGID
WARNING: Failed to find a character mapping for 73 in ArialMT
Jun 18, 2020 5:24:05 PM org.apache.pdfbox.pdmodel.font.PDType0Font toUnicode
WARNING: No Unicode mapping for CID+82 (82) in font ArialMT
[...]

Here's the code to reproduce the issue:

    PDDocument doc = new PDDocument();

    String fontPath = "C:\\Windows\\Fonts\\arial.ttf";
    PDFont font = PDType0Font.load(doc, new File(fontPath));

    PDPage page = new PDPage(PDRectangle.A4);
    doc.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page);
    contentStream.setFont(font, 8f);
    contentStream.beginText();
    contentStream.newLineAtOffset(75f, page.getMediaBox().getHeight() - 25f);
    contentStream.showText("foo");
    contentStream.endText();
    contentStream.close();    

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(doc));
    job.print();

I tried googling this error but all I can find is people trying to extract text from an existing document. That is not what I'm doing, I'm trying to create a document and print it.

What am I doing wrong?

Thanks.


Solution

  • You should save the file you created before printing it, and reload it for printing. The problem is happening here because the font subsetting is done when saving.