【发布时间】:2023-03-21 04:19:01
【问题描述】:
我正在尝试使用 wkhtmltopdf 生成 PDF,其中有图表 js。图表js版本3.4.x
我在下面写了html来生成图表
你好
js代码:
<script type="text/javascript">
const labels = [
'January',
'February',
'March',
'April',
'May',
'June',
];
const data = {
labels: labels,
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45],
}]
};
const config = {
type: 'bar',
data,
options: {
animation: false,
scales: {
x: {
grid: {
tickColor: 'red'
},
ticks: {
color: 'blue',
}
}
}
}
};
var myChart = new Chart(
document.getElementById('myChart').getContext('2d'),
config
);
</script>
图表在 html 中运行良好
下达命令后
wkhtmltopdf --javascript-delay 1000 http://localhost/pdf_test/index.html testpdf101
我得到了段落但没有图表。我测试了版本图表 js 2.9.4 的相同程序,它工作正常。为什么它在版本 3 中不起作用?如何为图表 js 版本 3.4.x 生成 pdf?
【问题讨论】:
-
尝试使用任何画布并检查画布是否呈现在 pdf 中。也许 wktmltopdf 不支持画布。如果是这样,请尝试将 chartJS 转换为图像,可能使用 Chart js docs 中指定的 Chart.toBase64Image()。如果您需要知道如何在 html 中呈现 base64 图像,请查看post
标签: javascript chart.js wkhtmltopdf