Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript jspdf-autotable: font is not working in live website

Abhishek6198

New Coder
I am trying to set a custom font inside styles{} inside doc.autoTable({ }). This is my code:

JavaScript:
    let doc = new jsPDF();

    doc.autoTable({ 
       html: '#news_pos', 
       startX: 10,
       startY: 15,
       theme: 'grid',
       bodyStyles: {lineColor: [0, 0, 0]},
       styles: { 
          font: "SakalBharati", //<------ This is the font. I have already added this font using doc.addfont()
          fontSize: 10,
          cellWidth: 'auto',
          halign: 'center',
          fillColor: [225, 197, 238]
       }, 
    })
    
    doc.save("file.pdf");

The above code is working perfectly when I run it in localhost. But now, when I've uploaded it into my live website, strangely, it's not working!! The code below works if I apply the font to a standard text (in both localhost and live website) like:

JavaScript:
    doc.setFont("SakalBharati","normal");
    doc.text(10,15,"पूसीरे के");

    /*This code works perfectly in both local and live server. Only
      when I apply the font in jspdf-autotable (in live server),
      it doesn't work*/

This is the table displayed when I execute the code in my local server
aoMdZ.png


And this is what I get when I execute the code in my live server
PXEkE.png


As the above code wasn't working in the live website, I tried a different approach:

What I did was I converted the text to an image and appended that image into the cell of my HTML table.

JavaScript:
    var tCtx = document.getElementById('textCanvas').getContext('2d'),
    imageElem = document.getElementById('image');
    tCtx.canvas.width = tCtx.measureText("पूसीरे के रेसुब ने नाबालिगों").width;
    tCtx.fillText("पूसीरे के रेसुब ने नाबालिगों", 0, 10);
    imageElem.src = tCtx.canvas.toDataURL();
    cell1.appendChild(imageElem);

And then appending this image into my table cell in jspdf-autotable:

JavaScript:
doc.autoTable({ 
       html: '#news_pos', 
       startX: 10,
       startY: 15,
       theme: 'grid',
       didDrawCell: function(hookData) {
         for(var j=1; j<document.getElementById("news_pos").rows.length; j++){
            for(var k=0; k<document.getElementById("news_pos").rows[j].cells.length; k++){
              if(document.getElementById("news_pos").rows[j].cells.item(k).innerHTML.includes("img")){
                if(hookData.column.index == k && hookData.row.index == j){
                   var td = hookData.cell.raw;
                   var img = td.getElementsByTagName('img')[0];
                   var dim = hookData.cell.height - hookData.cell.padding('vertical');
                   doc.addImage(img.src, hookData.cell.x+4,  hookData.cell.y+1, 80,dim+2);
                }
             }
          }
       }
     },
     bodyStyles: {lineColor: [0, 0, 0]}
    })


Suprisingly enough, this too shows the data inside the table perfectly, but only in my local server.

Why? What is the matter? Is this a bug that the font doesn't work in jspdf-autotable in live server? Why can't I show vernacular text in my jspdf autotable in my live website? Please help me fix this
 
I am trying to set a custom font inside styles{} inside doc.autoTable({ }). This is my code:

JavaScript:
    let doc = new jsPDF();

    doc.autoTable({
       html: '#news_pos',
       startX: 10,
       startY: 15,
       theme: 'grid',
       bodyStyles: {lineColor: [0, 0, 0]},
       styles: {
          font: "SakalBharati", //<------ This is the font. I have already added this font using doc.addfont()
          fontSize: 10,
          cellWidth: 'auto',
          halign: 'center',
          fillColor: [225, 197, 238]
       },
    })
   
    doc.save("file.pdf");

The above code is working perfectly when I run it in localhost. But now, when I've uploaded it into my live website, strangely, it's not working!! The code below works if I apply the font to a standard text (in both localhost and live website) like:

JavaScript:
    doc.setFont("SakalBharati","normal");
    doc.text(10,15,"पूसीरे के");

    /*This code works perfectly in both local and live server. Only
      when I apply the font in jspdf-autotable (in live server),
      it doesn't work*/

This is the table displayed when I execute the code in my local server
aoMdZ.png


And this is what I get when I execute the code in my live server
PXEkE.png


As the above code wasn't working in the live website, I tried a different approach:

What I did was I converted the text to an image and appended that image into the cell of my HTML table.

JavaScript:
    var tCtx = document.getElementById('textCanvas').getContext('2d'),
    imageElem = document.getElementById('image');
    tCtx.canvas.width = tCtx.measureText("पूसीरे के रेसुब ने नाबालिगों").width;
    tCtx.fillText("पूसीरे के रेसुब ने नाबालिगों", 0, 10);
    imageElem.src = tCtx.canvas.toDataURL();
    cell1.appendChild(imageElem);

And then appending this image into my table cell in jspdf-autotable:

JavaScript:
doc.autoTable({
       html: '#news_pos',
       startX: 10,
       startY: 15,
       theme: 'grid',
       didDrawCell: function(hookData) {
         for(var j=1; j<document.getElementById("news_pos").rows.length; j++){
            for(var k=0; k<document.getElementById("news_pos").rows[j].cells.length; k++){
              if(document.getElementById("news_pos").rows[j].cells.item(k).innerHTML.includes("img")){
                if(hookData.column.index == k && hookData.row.index == j){
                   var td = hookData.cell.raw;
                   var img = td.getElementsByTagName('img')[0];
                   var dim = hookData.cell.height - hookData.cell.padding('vertical');
                   doc.addImage(img.src, hookData.cell.x+4,  hookData.cell.y+1, 80,dim+2);
                }
             }
          }
       }
     },
     bodyStyles: {lineColor: [0, 0, 0]}
    })


Suprisingly enough, this too shows the data inside the table perfectly, but only in my local server.

Why? What is the matter? Is this a bug that the font doesn't work in jspdf-autotable in live server? Why can't I show vernacular text in my jspdf autotable in my live website? Please help me fix this
Hi there, might want to take a look at what languages/fonts your live-server is able to support. Might just be that the particular font is not supported by default. If that is the case, you may need the help of a google font script
 
Hi there, might want to take a look at what languages/fonts your live-server is able to support. Might just be that the particular font is not supported by default. If that is the case, you may need the help of a google font script
Yea, but if that were the case, then how come if I write the same text like this doc.text(10,15,"पूसीरे के");, then it shows the text in pdf. Only if the text is present inside the table, it shows as ?????????
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom