Generate Arabic content with PDFKit & nodeJS
Asked Answered
R

1

6

i'm using pdfkit with nodejs to generate dynamically PDF files. the generation works fine but i have a problem displaying arabic characters even if i setup a font that support arabic.

The letters are rendered correctly, but the words are displayed character by character :(

here's my code

doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
var str = "فصل الربيع الزهور \n#nature #payesage #fleurs #plantes #vert #espace #temara #rabat #maroc #WeekEnd #balade #instamoment #instalife #instamaroc #photographie #macro #peace";
doc.font('resources/HelveticaNeueLTArabic-Roman.ttf').text(str);

Any thoughts or suggestions will be great.

Refractor answered 14/5, 2015 at 23:42 Comment(1)
Just found out they are switching to fontkit for their font engine. Which will bring better support for Arabic languages.Spermatid
S
1

Use Amiri font , it supports arabic font

const customFontRegular = fs.readFileSync(`./amiri/Amiri-Regular.ttf`);
const customFontBold = fs.readFileSync(`./amiri/Amiri-Bold.ttf`);

pdfDoc.registerFont(`Amiri-Regular`, customFontRegular);
pdfDoc.registerFont(`Amiri-Bold`, customFontBold);

And it can be used as

pdfDoc.font('Amiri-Regular').text("Hello world");
Superbomb answered 5/12, 2021 at 12:20 Comment(2)
Can you explain where the variable fontBold in the second code block comes from?Deliquesce
You can specify the font name to fontBold variable , for example const fontBold="Amiri-Bold";Superbomb

© 2022 - 2024 — McMap. All rights reserved.