'Create PDF from HTML string jsPDF

I wanted to create a PDF document from the HTML string I get from SQL using jsPDF.

//HTML String (Part of)

<table style='width:100%'> <table class="test_listview" width="100%"><tr><td width="1%"></td><td width="20%";padding: 5px;  style="line-height: 20px;"><b>SG:</b></td><td width="28%";padding: 5px;  style="border-bottom: 1px solid #000;line-height: 20px;">Test</td></tr></table></table>

//Tried

var tag = document.createElement("p");
 tag.textContent = //Pasted the above HTML;
    const input = tag;
        html2canvas(input)
          .then((canvas) => {
            const imgData = canvas.toDataURL('image/png');
            const pdf = new jsPDF();
            pdf.addImage(imgData, 'JPEG', 0, 0);
            pdf.save("download.pdf");
          });

The compiler didn't work. So is there any way to make it work using jsPDf & htmlCanvas? Or any alternative to this??

Something like passing this HTML string and generating the PDF.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source