javascript - 如何将多个图表JS提取到多个页面中的pdf文件
问题描述
我正在使用下面的 JS 代码为我的多个图表生成一个 pdf 文件。但它将所有图表放在一个页面上。我的问题是如何修改它以将每个图表设置在单个页面中。以及如何在每个图表的 pdf 中键入标题。所以它是一个标题,然后是图表图像等等。
JS代码:
<script type="text/javascript">
$('#downloadPdf').click(function(event) {
// get size of report page
var reportPageHeight = $('#reportPage').innerHeight();
var reportPageWidth = $('#reportPage').innerWidth();
// create a new canvas object that we will populate with all other canvas objects
var pdfCanvas = $('<canvas />').attr({
id: "canvaspdf",
width: reportPageWidth,
height: reportPageHeight
});
// keep track canvas position
var pdfctx = $(pdfCanvas)[0].getContext('2d');
var pdfctxX = 0;
var pdfctxY = 0;
var buffer = 100;
// for each chart.js chart
$("canvas").each(function(index) {
// get the chart height/width
var canvasHeight = $(this).innerHeight();
var canvasWidth = $(this).innerWidth();
// draw the chart into the new canvas
pdfctx.drawImage($(this)[0], pdfctxX, pdfctxY, canvasWidth, canvasHeight);
pdfctxX += canvasWidth + buffer;
// our report page is in a grid pattern so replicate that in the new canvas
if (index % 2 === 1) {
pdfctxX = 0;
pdfctxY += canvasHeight + buffer;
}
});
// create new pdf and add our new canvas as an image
var pdf = new jsPDF('l', 'pt', [reportPageWidth, reportPageHeight]);
pdf.addImage($(pdfCanvas)[0], 'PNG', 0, 0);
// download the pdf
pdf.save('filename.pdf');
});
</script>
解决方案
推荐阅读
- ios - 调用中的额外参数“用户名”-WatsonCloud SpeechToTextV1
- c# - CreatedAtRoute 生成一个异常:“没有路由与提供的值匹配。”
- javascript - 使用材质 UI 和反应测试库时从对话框测试 onClose 回调?
- jenkins - 如何在 Jenkins 管道中注入阶段或步骤
- python - 使用 DRF 的多模型文件上传器
- sql - 通过使用不同的列在带有组的选择查询中包含一个附加列
- php - PHP,Laravel - 将字符串连接到模型
- kernel - 为高斯核平滑器选择带宽和大小窗口
- docker - 任何限制正在运行的 docker 容器的 net io 的方法
- c# - 对 Azure Active Directory 的身份验证并收到 AADSTS90019 错误