Pdfkit: Strange character "脨" every time I press "intro"

Created on 15 Jan 2017  路  10Comments  路  Source: foliojs/pdfkit

Hello,
I'm trying to get a text from an object in NodeJs but I have a little problem:
For example:
doc.text(myObject.text);
if myObject.text is like (with "intro's"):
"THIS
IS
MY
TEXT"

when I render the PDF it looks like:
"THIS 脨
IS 脨
MY 脨
TEXT"

I've been looking for info, but I didn't found anything about this, am I the only person in the world??

bug fonts

Most helpful comment

Thanks, with these details the bug is easy to reproduce:
doc.text('H\r\nE\r\nL\r\nL\r\nO');
Apparently the character 13 (carriage return, \r) is not processed correctly by PDFKit, when using standard fonts.

As a workaround you can clean the line breaks with a regular expression:
string.replace(/\r\n|\r/g, '\n')

All 10 comments

There are probably strange invisible unicode characters in your text...

Mmmm... but in console the text is clean, with no symbpls.

It can be some kind of space character and _look_ cleen.
Just to be sure, check it with charCodeAt()

for (i = 0; i < string.length; i++) {
  console.log(string.charCodeAt(i));
}

Which font are you using?

Thanks for your answer alafr,
I'm using the default font of PDFkit. I tried your code with:
H(intro)E(intro)L(intro)L(intro)O
the result was:
72, 13, 10, 69, 13, 10, 76, 13, 10, 76, 13, 10, 79

Thanks, with these details the bug is easy to reproduce:
doc.text('H\r\nE\r\nL\r\nL\r\nO');
Apparently the character 13 (carriage return, \r) is not processed correctly by PDFKit, when using standard fonts.

As a workaround you can clean the line breaks with a regular expression:
string.replace(/\r\n|\r/g, '\n')

It works pretty fine!
Thank you @alafr !

Although @alafr has found a workaround would it be possible to have a more permanent fix?

Just for the record this issue is still reproducible and the fix above by @alafr to replace the \r\n carriage return with just \n new line.

although @alafr solution does seem to work on the output, if you have the pdf read by a reader (i.e. pdf.js) it will return a "UnkownErrorException" - " bad XRef entry"

although @alafr solution does seem to work on the output, if you have the pdf read by a reader (i.e. pdf.js) it will return a "UnkownErrorException" - " bad XRef entry"

Hi,
i think the XRef error comes from somewhere else. I load my data from a mysql database, change '\r\n' to '\n' and my PDF can be loaded without errors by any common reader (Adobe, PDF.js).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huy-nguyen picture huy-nguyen  路  4Comments

GuiRitter picture GuiRitter  路  3Comments

oknoorap picture oknoorap  路  4Comments

tadarao picture tadarao  路  5Comments

raesche picture raesche  路  4Comments