Generate pdf from HTML in div using Javascript
#1
I've been working on a project that requires the conversion of specific parts of an HTML page into a PDF document using JavaScript. The challenge I'm facing is that while I've been using jsPDF, it seems to only support adding text to a PDF, and not directly converting HTML. I'd appreciate some guidance on how I can capture the HTML content within a specific div and render it as it looks on the web page into a PDF document. Here is a snippet of the HTML I want to convert:

Code:
<
!DOCTYPE html >
    <
    html >
    <
    body >
    <
    p > don 't print this to pdf</p> <
    div id = "pdf" >
    <
    p > < font size = "3"
color = "red" > print this to pdf < /font></p >
    <
    /div> <
    /body> <
    /html>

The goal is to select only the content within the div with the id "pdf" and have it rendered in the PDF. Additionally, I'd like the PDF to be automatically downloaded with the filename 'foobar.pdf'. If anyone has experience with converting HTML to PDF on the client side, please let me know how to achieve this.
Reply
#2
One possible solution to your problem is to use the html2canvas library in combination with jsPDF. This way, you can take a "screenshot" of your HTML content and add it to your PDF. The html2canvas library will handle rendering the HTML as a canvas element, and then you can pass that canvas to jsPDF to add it to your PDF document. Here's how you can do it:
First, make sure you have both jsPDF and html2canvas available in your project. You can include them via CDN like this:

Code:
<
script src = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js" > < /script> <
    script src = "https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js" > < /script>

Next, implement the following JavaScript code to capture the #pdf div and initiate the download:

Code:
const element = document.getElementById('pdf');
    html2canvas(element).then((canvas) => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF.jsPDF();
        pdf.addImage(imgData, 'PNG', 0, 0);
        pdf.save('foobar.pdf');
    });
});

Remember that you need to call this code at a point when you're ready to convert the HTML to PDF, such as in response to a user action like clicking an Export button.
Reply
#3
That's a good approach, dev_hero_72. However, we need to be careful with sizing and layout as it may not always match the expectations if the content in the `#pdf` div is larger than a single PDF page or has complex styles. Also, we should provide a fallback or error handling in case the canvas or PDF generation fails. Additionally, one might want to specify the scale for html2canvas to improve the resolution of the resulting PDF.
Here is an enhanced version of your code with an option to set the scale:

Code:
const element = document.getElementById('pdf');
    html2canvas(element, {
        scale: 2
    }).then((canvas) => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF.jsPDF();
        pdf.addImage(imgData, 'PNG', 0, 0, 210, 297); // A4 size page of PDF
        pdf.save('foobar.pdf');
    }).catch(error => {
        console.error('Could not generate PDF: ', error);
    });
});
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)