0
votes

I am trying to save entire flot (Pie/Bar)Chart to PDF. I am using jQuery/FLOT to draw a Pie and Bar Chart.

I have the code for downloading the flot chart as PDF but I have three charts in one page and the problem is once I click download they printed in the page and in the same time I got them in different PDF separately. My question is it possible to have them all in one pdf and without printing them in the page.

Any idea ?

Thank you.

Here is my code:

 <div id="container" style="width:330px;height:330px"></div>

  <a id="toPdf">Download as PDF </a>

  var _canvas = null;

   $(function() {
   $.plot($("#container"), [ { label: 'Test', data: [[0, 0], [1, 9]] } ], { yaxis: { max: 1 } });
   $("#toPdf").on("click", function(e) {
    e.preventDefault();

        html2canvas($("#container").get(0), {
            onrendered: function(canvas) {
                document.body.appendChild(canvas);

                var imgData = canvas.toDataURL('image/png');
                console.log('Report Image URL: '+imgData);
                var doc = new jsPDF('landscape');

                doc.addImage(imgData, 'PNG', 10, 10, 190, 95);
                doc.save('testingPDF.pdf');
            }
        });
         });
      });
1

1 Answers

1
votes

Have you tried removing the canvas once the work is done?

$(function() {
    $.plot($("#container"), [ { label: 'Test', data: [[0, 0], [1, 9]] } ], { yaxis: { max: 1 } });
    $("#toPdf").on("click", function(e) {
    e.preventDefault();

        html2canvas($("#container").get(0), {
            onrendered: function(canvas) {
                document.body.appendChild(canvas);

                var imgData = canvas.toDataURL('image/png');
                console.log('Report Image URL: '+imgData);
                var doc = new jsPDF('landscape');

                doc.addImage(imgData, 'PNG', 10, 10, 190, 95);
                doc.save('testingPDF.pdf');
                document.body.removeChild(canvas); //  newly added line
            }
        });
    });
});

UPDATE

Check out this new fiddle (you wanted something link this right?)

HTML

<div class="container1" style="width:330px;height:330px"></div>
<div class="container2" style="width:330px;height:330px"></div>
<div class="container3" style="width:330px;height:330px"></div>
<a id="toPdf">Generate to pdf </a>

CODE

var _canvas = null;

$(function () {
    $.plot($(".container1"), [{
        label: 'Testing1',
        data: [
            [0, 0],
            [1, 9]
        ]
    }], {
        yaxis: {
            max: 1
        }
    });
    $.plot($(".container2"), [{
        label: 'Testing2',
        data: [
            [0, 0],
            [1, 9]
        ]
    }], {
        yaxis: {
            max: 1
        }
    });
    $.plot($(".container3"), [{
        label: 'Testing3',
        data: [
            [0, 0],
            [1, 9]
        ]
    }], {
        yaxis: {
            max: 1
        }
    });

    window.allcanvas = [];

    $("#toPdf").on("click", function (e) {
        e.preventDefault();
        var allcontainers = $('[class^="container"]');
        allcontainers.each(function (index, elem) {
            html2canvas($(elem).get(0), {
                onrendered: function (canvas) {
                    window.allcanvas.push(canvas);
                    if(allcontainers.length == allcanvas.length){
                        finalpdf();
                    }
                }
            });
        });
    });
    finalpdf = function(){
        var doc = new jsPDF('landscape');
        for(var i = 0; i<allcanvas.length;i++){
            var imgData = allcanvas[i].toDataURL('image/jpeg');
            doc.addImage(imgData, 'JPEG', 10, 10, 190, 95);
            if(i != allcanvas.length-1)
                doc.addPage();
        }
        doc.save('testingPDF.pdf');
    };
});