1

Does pdfkit support Hindi language? I am writing a program to generate pdf document with Hindi text

Code sample

let user = { 
    no: 1,
    name: 'अर्जुन दाव',
    age: 26,
    gender: 'पुरुष'
};

console.log(user);

const doc = new PDFDocument();
        
doc.fontSize(36)
.fillColor('red')
.text(user.name, 50, 400);
        
doc.fontSize(16)           
.fillColor('black')            
.text(`उम्र       : ${user.age}, ${user.gender}`, 50, 460);

doc.pipe(fs.createWriteStream(`./output/${user.no}.pdf`));
doc.end();

Output

It prints the user object in the console as expected. But the pdf generated with shows a bunch of weird characters in place of texts.

generated pdf

environment

  • pdfkit version: ^0.11.0
  • Node version: v10.19.0
  • Operating System: ubuntu 20.0.4

What am I doing wrong?

Bopsi
  • 2,090
  • 5
  • 36
  • 58

1 Answers1

0

Download https://fonts.google.com/specimen/Tiro+Devanagari+Hindi?query=hindi and add in your code:

doc.font('./fonts/Tiro_Devanagari_Hindi/TiroDevanagariHindi-Regular.ttf');

That worked for me with Hindi input text without converting it to English or using transliteration.

Aditya Mittal
  • 1,681
  • 16
  • 12