javascript - 如何使用jspdf获取整个div并导出为pdf?

标签 javascript jquery pdf jspdf

我只有一个大 div,其中包含大量图表、表格,还有一些来自数据库的结果。我需要将其导出为 pdf 文件。我正在使用jspdf。

<script type="text/javascript">
        function genPDF(){
            var canvas = $("#chart_div" .canvasjs-chart-canvas).get(0);

            var canvasImg = canvas.toDataURL("image/jpeg", 1.0);
            var doc = new jsPDF(
                {
                orientation: 'l',
                unit: 'px',
                format: 'a4'
            }
            );
            doc.text(15, 15, "Cool Chart");
            doc.addImage(canvasImg, 'JPEG', 10, 10, 280, 150);
            doc.fromHTML($('#pdfDiv').get(0),{
                'width':500
            });

            doc.save('test.pdf');
        }
    </script>

我认为我的问题是将该chart_div 转换为JPEG。当我输入这行代码时它不起作用

 var canvasImg = canvas.toDataURL("image/jpeg", 1.0);

我是 JavaScript 初学者。各位帮帮忙

最佳答案

我对 jsPDF 不太熟悉,但你必须做这样的事情......

[CodePen Mirror]

/**
 * SCROLL TO THE BOTTOM OF THIS CODE TO SEE THE EXPORT
 *
 */

var chart = new CanvasJS.Chart("chartContainer", {
  title: {
    text: "Exporting chart using jsPDF & toDataurl"
  },
  data: [{
    type: "spline",
    dataPoints: [{
        x: 10,
        y: 4
      },
      {
        x: 20,
        y: 7
      },
      {
        x: 30,
        y: 2
      },
      {
        x: 40,
        y: 3
      },
      {
        x: 50,
        y: 5
      }
    ]
  }]
});
chart.render();

var chart = new CanvasJS.Chart("chartContainer2", {
  animationEnabled: true,
  exportEnabled: true,
  theme: "light1", // "light1", "light2", "dark1", "dark2"
  title: {
    text: "Simple Column Chart with Index Labels"
  },
  data: [{
    type: "column", //change type to bar, line, area, pie, etc
    //indexLabel: "{y}", //Shows y value on all Data Points
    indexLabelFontColor: "#5A5757",
    indexLabelPlacement: "outside",
    dataPoints: [{
        x: 10,
        y: 71
      },
      {
        x: 20,
        y: 55
      },
      {
        x: 30,
        y: 50
      },
      {
        x: 40,
        y: 65
      },
      {
        x: 50,
        y: 92,
        indexLabel: "Highest"
      },
      {
        x: 60,
        y: 68
      },
      {
        x: 70,
        y: 38
      },
      {
        x: 80,
        y: 71
      },
      {
        x: 90,
        y: 54
      },
      {
        x: 100,
        y: 60
      },
      {
        x: 110,
        y: 36
      },
      {
        x: 120,
        y: 49
      },
      {
        x: 130,
        y: 21,
        indexLabel: "Lowest"
      }
    ]
  }]
});
chart.render();

var button = document.getElementById("btnDownload");

function generatePDF() {
  var canvases = [];
  canvases.push(document.querySelector("#chartContainer .canvasjs-chart-canvas"));
  canvases.push(document.querySelector("#chartContainer2 .canvasjs-chart-canvas"))

  var pdf = new jsPDF();
  pdf.setFontSize(12);
  let count = 0

  canvases.forEach(canvas => {
    var dataURL = canvas.toDataURL('image/jpeg');
    /* ************ */
    if (count > 0) pdf.addPage(canvas.style.width, canvas.style.height); // had to do this
    count++; // because the first page was blank... I only add new pages after first is created
    /* ************* */
    pdf.addImage(dataURL, 'JPEG', 15, 40, 180, 180);
  })

  pdf.save("download.pdf");
}

button.addEventListener("click", generatePDF);
#btnDownload {
  width: 300px;
  height: 35px;
}
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.min.js"></script>

<br/><hr/>
<h3>Click me to download PDF</h3>
<button id="btnDownload"> Download PDF </button>
<br/>
<hr/><br/>
<div id="chartContainer" style="height: 300px; width: 300px;"></div>
<div id="chartContainer2" style="height: 300px; width: 300px;"></div>

关于javascript - 如何使用jspdf获取整个div并导出为pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553728/

相关文章:

javascript - 在客户端过滤嵌套对象

javascript - 无法使用 .each 获取 id

Android:如何使用 intent-filter 处理 PDF 电子邮件附件?

pdf - TestCafe:测试在新选项卡中打开 PDF 的页面

javascript - 如何只选择一个复选框而不是能够选择多个复选框? ( react )

javascript - 单页应用程序无法获取 Facebook 共享的当前页面元标记

javascript - 扩展 HTMLTextAreaElement 的 HTML5 ES6 自定义元素使非法构造函数崩溃

javascript - 如何使用淡入淡出更改背景图像

jquery - 语法错误,无法识别的表达式 : #[object HTMLElement] Error

java - 从 Java 打印多个 PDF 作为单个打印作业(物理打印)