Skip to content
Advertisement

Arabic Text issue with PDFKit plugin

To generate dynamic PDF files, I’m using PDFKit. The generation works fine, but I’m having trouble displaying arabic characters, even after installing an arabic font. Also, Arabic text is generated correctly, but I believe the word order is incorrect.

As an example,

I’m currently using pdfkit: “0.11.0”

Text: مرحبا كيف حالك ( Hello how are you )

Font: Amiri-Regular.ttf

const PDFDocument = require("pdfkit");
var doc = new PDFDocument({
  size: [595.28, 841.89],
  margins: {
    top: 0,
    bottom: 0,
    left: 0,
    right: 0,
  },
});
const customFont = fs.readFileSync(`${_tmp}/pdf/Amiri-Regular.ttf`);
doc.registerFont(`Amiri-Regular`, customFont);
doc.fontSize(15);
doc.font(`Amiri-Regular`).fillColor("black").text("مرحبا كيف حالك");
doc.pipe(fs.createWriteStream(`${_tmp}/pdf/arabic.pdf`));
doc.end();

OUTPUT:

PDF with arabic text

Advertisement

Answer

this problem allowed me to go through here, but unfortunately I am not convinced by the answers posted and even add a library to change the direction of the text with pdfkit. after several minutes on the pdfkit guide docs, here is the solution:

doc.text("مرحبا كيف حالك", {features: ['rtla']})
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement