Search code examples
javascriptjqueryhtmljspdfhtml2canvas

Generate High Quality PDF with Javascript (jspdf+html2canvas)


I've been trying to convert and generate PDF from HTML page with click of a button on the page that automatically generates and force download a PDF of the page using two popular addons * JSPDF * HTML2Canvas So far everything works fine but the PDF generated are always blurry and not high quality after importing the js files (jquery, html2canvas, jspdf). Here is my javascript code

function CreatePDFfromHTML() {
    var HTML_Width = $(".html-content").width();
    var HTML_Height = $(".html-content").height();
    var top_left_margin = 15;
    var PDF_Width = HTML_Width + (top_left_margin * 2);
    var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
    var canvas_image_width = HTML_Width;
    var canvas_image_height = HTML_Height;

    var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;

    html2canvas($(".html-content")[0]).then(function (canvas) {
        var imgData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
        pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
        for (var i = 1; i <= totalPDFPages; i++) { 
            pdf.addPage(PDF_Width, PDF_Height);
            pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
        }
        pdf.save("invoice_<?php echo $trackingNumber ;?>.pdf");
        $(".html-content").hide();
    });
}

How do i adjust the code to make html2canvas convert the page to a better quality image (get rid of the blur and make the PDF file generated even better)


Solution

  • After days of searching i finally found a way to make the quality better by multiplying the HTML_Width and HTML_height by factor of 3. Here is the new code:

    function CreatePDFfromHTML() {
        var HTML_Width = $(".html-content").width()*3;
        var HTML_Height = $(".html-content").height()*3;
        var top_left_margin = 15;
        var PDF_Width = HTML_Width + (top_left_margin * 2);
        var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
        var canvas_image_width = HTML_Width;
        var canvas_image_height = HTML_Height;
    
        var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
    
        html2canvas($(".html-content")[0]).then(function (canvas) {
            var imgData = canvas.toDataURL("image/jpeg", 1.0);
            var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
            pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
            for (var i = 1; i <= totalPDFPages; i++) { 
                pdf.addPage(PDF_Width, PDF_Height);
                pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
            }
            pdf.save("invoice_<?php echo $trackingNumber ;?>.pdf");
            $(".html-content").hide();
        });
    }