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-Unable to write updated values in pdf

Abhishek6198

New Coder
I am trying to print an HTML table in my pdf. This is my code:

JavaScript:
    let a = 0;
    let b = 0;
    let c = 0;
    
    let table = document.getElementById('pol_table');
    const doc = new jsPDF();
    
    date.onchange = evt => {
        $.ajax({
            url: '/server.php',
            type: 'POST',
            data:{ "input":"fetch data", "date": evt.target.value},
            success: function(response){
                const arr = JSON.parse(response);
                a = arr[0];
                b = arr[1];
                c = arr[2];
            },
            complete: function(){}
        })
    }
    
   document.getElementById('generate').addEventListener('click',async function(){
        if(table.rows.length>0){
            while(table.rows.length!=0){
                table.deleteRow(0);
            }
        }
        var row = table.insertRow(0);
        var cell = row.insertCell(0);
        var cell1 = row.insertCell(1);
        var cell2 = row.insertCell(2);
        var cell3 = row.insertCell(3);
    
        cell.style.textAlign = "center";
        cell1.style.textAlign = "center";
        cell2.style.textAlign = "center";
        cell3.style.textAlign = "center";
    
        cell.innerHTML = "<b>Sl</b>";
        cell1.innerHTML = "<b>Positive</b>";
        cell2.innerHTML = "<b>Negative</b>";
        cell3.innerHTML = "<b>Neutral</b>";
    
        var row1 = table.insertRow(1);
        var cell = row1.insertCell(0);
        var cell1 = row1.insertCell(1);
        var cell2 = row1.insertCell(2);
        var cell3 = row1.insertCell(3);
    
        cell.style.textAlign = "center";
        cell1.style.textAlign = "center";
        cell2.style.textAlign = "center";
        cell3.style.textAlign = "center";
    
        cell.innerText = "1";
        cell1.innerText = a;
        cell2.innerText = b;
        cell3.innerText = c;
    
        console.log(table); //shows the updated table every time
    
    /*----THE UPDATED TABLE DOESN'T REFLECT HERE---------*/
        doc.autoTable({
            html: '#pol_table',
            startX: 10,
            startY: 15,
            theme: 'grid',
            bodyStyles: {lineColor: [0, 0, 0]},
            styles: {
                cellWidth: 'auto',
                fontSize: 15
            }
        })
    /*------------------------------------------*/

    /*------------------------------------------------------------*/
        doc.text(10,15,a+" "+b+" "+" "+c); //this also outdated
        doc.save("report.pdf");
    /*-------------------------------------------------------------*/
    })


----------
  • I select a date and generate pdf, it shows the correct data.
  • Now I select another date and generate pdf, it shows the data fetched for the previous date.

I don't get this. My HTML table is updated every time the response is fetched from my server.php. So why is the previous data always printed in my pdf?
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom