Getting "PubSub Error Cannot read property 'widths' of undefined TypeError: Cannot read property 'widths' of undefined"

Viewed 138

I want to add custom font to jspdf document. I am loading the file , converting it into base64 and then adding the font toVFS. But I face the following error when I try to add the font and eventually the text to the pdf document.
PubSub Error No unicode cmap for font Error: No unicode cmap for font
Here is code:

getBase64 = (file)=>{
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        resolve(reader.result);
      };
      reader.onerror = error => reject(error);
    });
  };

  downLoadPDF(){
    console.log("download pdf");
    let x= 10,y=40, page = 1;
    var doc = new jsPDF("p", "mm", "a4");
    doc.setFontSize(10);
    doc.text("Page "+page++ , 185, 6);
    var client = new XMLHttpRequest();
    let inst = this;
    client.open('GET', 'assets/fonts/Roboto-Bold.ttf');
    client.onloadend = function() {
      var fileData:any = client.responseText;
      inst.getBase64(new Blob([fileData])).then((data:string)=>{
//Was getting "jsPDF PubSub Error Invalid string. Length must be a multiple of 4 Error: Invalid //string. Length must be a multiple of 4" error. So added the below line.
        data = data + '='.repeat(4 - data.length % 4);
        doc.addFileToVFS("Roboto-Bold.ttf",data);
        doc.addFont("Roboto-Bold.ttf","Roboto-Bold","normal");
        doc.setFont("Roboto-Bold","normal");
//Here, I get the following error:
    //jsPDF PubSub Error No unicode cmap for font Error: No unicode cmap for font
    //at i.registerTTF (jspdf.min.js:262)
    //at new i (jspdf.min.js:262)
    //at Function.i.open (jspdf.min.js:262)
    //at Object.<anonymous> (jspdf.min.js:173)
    //at oe.publish (jspdf.min.js:29)
    //at Object.yt (jspdf.min.js:29)
    //at Object.l.addFont (jspdf.min.js:29)
        doc.text("Company Name: Avaya",x,y);y+=10;
      });
    }
    client.send();    
   }
0 Answers
Related