PDF kit reverse numbers in Arabic fonts

43 Views Asked by At

I'm using pdfkit package for node js application to generate PDFs, but I faced a problem when I attempted to generate Arabic text. It always reverses the numbers in Arabic texts.

if I want to write: ساڵی 1992 (The year 1992), it always changes to (ساڵی ٢٩٩١), This issue persists even when I use Unicode custom fonts.

This is an example in image:

This is an example in image

Here are my codes:

function generatePDFForRuleAndRegulation(data) {
  const fileNamePDF = `${data.name}.pdf`;
  const doc = new PDFDocument();

  // Pipe the PDF document to a writable stream (file in this case)
  const stream = fs.createWriteStream(fileNamePDF);

  doc.pipe(stream);

  doc
    .font(`${path.join(__dirname, "../../assets/fonts/NRT-Reg.ttf")}`)
    .fontSize(12)
    .text(`${data.name}`, {
      align: "center",
    });
  doc.moveDown(); // Move cursor down

  doc
    .font(`${path.join(__dirname, "../../assets/fonts/Rabar_021.ttf")}`)
    .fontSize(10)
    .text(data.text, { align: "right", features: ["rtla"] });

  // Finalize the PDF document
  doc.end();

  // Inform when the file has been created
  stream.on("finish", () => {
    console.log(`PDF created: ${fileNamePDF}`);
  });
}

Is there any solution or workaround you can suggest? Alternatively, do you know of another package to generate PDFs that also supports Arabic fonts?

1

There are 1 best solutions below

1
Rawand On BEST ANSWER

Finally, I used custom RTL instead of pdfkit plugin.

// Function to check if text contains Hebrew characters
const isHebrew = (text) => {
  return text.search(/[\u0590-\u05FF]/) >= 0;
};

// Function to check if text contains Arabic characters
const isArabic = (text) => {
  return text.search(/[\u0600-\u06FF]/) >= 0;
};

// Function to reverse the order of words if the text is in Hebrew or Arabic
const rightToLeftText = (text) => {
  if (isHebrew(text) || isArabic(text)) {
    return text.split(" ").reverse().join(" ");
  } else {
    return text;
  }
};

// Example usage:
// Apply the rightToLeftText function to the "data.text" and store the result in the "text" variable
const text = rightToLeftText(data.text);

// Use a PDF generation library (assumed to be "doc") to set font, size, and text alignment
doc
  .font(`${path.join(__dirname, "../../assets/fonts/Rabar_021.ttf")}`)
  .fontSize(10)
  .text(text, { align: "right" });