javascript - 将流程图另存为 PDF

标签 javascript jquery pdf charts flot

我正在尝试将整个图表(饼图/条形图)保存为 PDF。我正在使用 jQuery/FLOT 绘制饼图和条形图。

我有用于将流程图下载为 PDF 的代码,但我在一页中有三个图表,问题是一旦我单击下载,它们就会打印在页面中,同时我会分别以不同的 PDF 形式获取它们。 我的问题是,是否可以将它们全部包含在一份 pdf 中,而不将它们打印在页面中。

有什么想法吗?

谢谢。

这是我的代码:

 <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');
            }
        });
         });
      });

最佳答案

工作完成后,您是否尝试过移除 Canvas ?

$(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
            }
        });
    });
});

更新

看看这个新的 fiddle (您想要这个链接吗?)

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>

代码

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');
    };
});

关于javascript - 将流程图另存为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27270313/

相关文章:

JavaScript:关于 JSON 数据操作的 2 个问题

jquery - 如何使用多个互相连接的KendoListBox

jquery - 改进 JQuery MP3 播放器插件代码

javascript - 折叠使用 vuejs 渲染的表格中的空单元格

javascript - Internet Explorer 是否支持 arguments.callee.name?

javascript - pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);

javascript - 如何改变值依赖于css动画?

pdf - 如何将多页 PDF 转换为单页 TIFF

django - 如何在 django 中使用 pdf.js

iOS WKWebView 在 DarkMode 中加载 PDF 数据在 PDF 文档周围有黑色背景