17

When trying to print a PDF page using Java and the org.apache.pdfbox library, I get this error:

PDFBOX : U+000A ('controlLF') is not available in this font Helvetica encoding: WinAnsiEncoding

glennsl
  • 28,186
  • 12
  • 57
  • 75
ibercode
  • 1,264
  • 3
  • 17
  • 29
  • Related: [No glyph for U+000D in font Helvetica](https://stackoverflow.com/questions/43738550/no-glyph-for-u000d-in-font-helvetica) – mkl Oct 09 '17 at 14:43
  • If you want to print INTO pdf, use the TextToPDF tool, or use its source code. – Tilman Hausherr Dec 11 '17 at 11:33

8 Answers8

16

[PROBLEM] The String you are trying to display contains a newline character.

[SOLUTION] Replace the String with a new one and remove the newline:

text = text.replace("\n", "").replace("\r", "");
glennsl
  • 28,186
  • 12
  • 57
  • 75
ibercode
  • 1,264
  • 3
  • 17
  • 29
  • 1
    Depending on the use case at hand, you might want to replace with a single space instead of an empty string. – mkl Oct 09 '17 at 14:39
  • I am not using \n but still I see the below error java.lang.IllegalArgumentException: U+FFFD ('.notdef') is not available in this font's encoding: WinAnsiEncoding Error if text is : Habitación but works fine when : Habitaci\u00F3n – Sandeep M Feb 06 '19 at 06:35
  • With \r it's not working better use
    if You are concatenating strings. For \r error is: java.lang.IllegalArgumentException: U+000D ('controlCR') is not available in this font's encoding: WinAnsiEncoding
    – Piotr Żak Dec 13 '19 at 14:42
11

The answer selected for this post works, replacing all instances of \n and \r from your string, if you know that it's a \n or \r character that's causing your problem. I've discovered that there are lots of different characters that will produce this error. Here's a sampling of those that I've found:

U+2010 ('hyphentwo') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2033 ('second') is not available in this font Helvetica encoding: WinAnsiEncoding
U+00A0 ('nbspace') is not available in this font Helvetica encoding: WinAnsiEncoding
U+FFFD ('.notdef') is not available in this font Helvetica encoding: WinAnsiEncoding
U+03BC ('mugreek') is not available in this font Helvetica encoding: WinAnsiEncoding
U+039C ('Mu') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2212 ('minus') is not available in this font Helvetica encoding: WinAnsiEncoding
U+0141 ('Lslash') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2103 ('centigrade') is not available in this font Helvetica encoding: WinAnsiEncoding
U+25AA ('H18543') is not available in this font Helvetica encoding: WinAnsiEncoding

In my case, I've simply chosen to remove any special character that's not included in my font. I used the solution from this page:

Remove illegal characters from string with PDFBox

K J
  • 8,045
  • 3
  • 14
  • 36
Stephen
  • 1,977
  • 2
  • 15
  • 19
3

I was running into a similar issue too (using pdfbox 2.0.11), my error was:

U+00A0 ('nbspace') is not available in this font Helvetica encoding: WinAnsiEncoding

Which was strange because checking the WinAnsiEncoding for the char name (int value 160) returned space, but the internal classes of pdfbox was returning the name nbspace.

The solution for me was upgrading to pdfbox 2.0.21.

andre
  • 1,084
  • 11
  • 13
  • 2
    That was this bug: https://issues.apache.org/jira/browse/PDFBOX-4891 There is a follow-up bug that will be fixed in 2.0.22 https://issues.apache.org/jira/browse/PDFBOX-4944 – Tilman Hausherr Sep 24 '20 at 10:17
2

If you'd like to retain the newline addition, i.e you indeed want your text to split and appear the later part in new line, then you can simply replace the \n with an HTML break tag, like below below .

return text.replace("\n","<br>");

:)

Abhinav Ganguly
  • 276
  • 4
  • 7
1

if you are trying to set a new line using "\n" in a string . you can try PDPageContentStream.newLineAtOffset(x,y) to add a new line

  PDFont font =  PDType1Font.HELVETICA ; 

  PDDocument doc    = new PDDocument();
  PDPage page = new PDPage();
  PDPageContentStream content = new PDPageContentStream(doc, page);
  content.beginText();
  content.moveTextPositionByAmount(10, 700);
  content.setFont(font, 12);
  content.drawString("start text   ");
  content.newLineAtOffset(0, -15);
  content.drawString("text in new line  ");        
  content.endText();
  content.close();
  doc.addPage(page);
  doc.save("file.pdf");

and the pdf enter image description here

Giovanni Contreras
  • 2,345
  • 1
  • 13
  • 22
0

Sometimes you have to change a font like :

PDFont font = PDType0Font.load(document, new File("C:\\Users\\dw\\Desktop\\FZLTXHJW.TTF"));

Replace "FZLTXHJW.TTF" with the font you have and it should support your text encoding.

What Dong
  • 19
  • 4
0

To remove all chars that can not be encoded by the font you use for PDF writing you can do something like:

PDType1Font font = PDType1Font.HELVETICA;    
public static void erasesControlCharacters(List<String> values) {
    String charSet = font.getFontDescriptor().getCharSet();
    for (int i = 0; i < values.size(); i++) {

        StringBuilder b = new StringBuilder();
        String test = values.get(i);
        for (int charIndex = 0; charIndex < test.length(); charIndex++) {
            if (WinAnsiEncoding.INSTANCE.contains(test.charAt(charIndex)) && charSet.contains(test.substring(charIndex, charIndex + 1))) {
                b.append(test.charAt(charIndex));
            }
        }
        values.set(i, b.toString());
    }
}
Peer
  • 3
  • 3
-2

One of thing we found and helped is -- while you are making a HTTP call with this special encoding data to an external system - make sure you encode your JSON (if json) body to utf-8 charset before doing a API post. something like this below

httpPost.setEntity(new StringEntity(bodyJsonParams, "UTF-8"));
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19