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??
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).
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')