yii2 - Chartjs does not show on pdf in yii2 despite showing in html view
问题描述
I have developed an app using Yii2, mpdf and chartjs. The page works well in html but doesn't show the chart when I export to pdf using mpdf. How can I solve this?
<?= ChartJs::widget([
'type' => 'line',
'options' => [
'height' => 400,
'width' => 400,
'show_as_html'=> true
],
'data' => [
'labels' => ["January", "February", "March", "April", "May", "June", "July"],
'datasets' => [
[
'label' => "Sales",
'backgroundColor' => "rgba(255,99,132,0.2)",
'borderColor' => "rgba(255,99,132,1)",
'pointBackgroundColor' => "rgba(255,99,132,1)",
'pointBorderColor' => "#fff",
'pointHoverBackgroundColor' => "#fff",
'pointHoverBorderColor' => "rgba(255,99,132,1)",
'data' => [28, 48, 40, 19, 96, 27, 100]
]
]
]
]);
?>
解决方案
You can see in the MPDF docs: https://mpdf.github.io/html-support/html-tags.html
that canvas
and script
tags are not supported. in the case of charts, most use canvas, you can see that chartsjs also uses canvas to render its charts. https://www.chartjs.org/docs/latest/general/responsive.html
Most PDF generator does not support rich HTML and charts.
I suggest you use phantomJS
for generating pdf.
http://jonnnnyw.github.io/php-phantomjs/
for PDF: http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/#output-to-pdf
推荐阅读
- elixir - 在 Elixir 中,我如何格式化相对于现在的 DateTime?
- spring - Tomcat 运行不同的应用程序
- azure-web-app-service - 如何在 azure webapp 中保存 json 文件以用作环境
- python - 尝试使用 http 创建代理检查器
- ffmpeg - 将字幕刻录到 mp4 上,使其始终显示且无法关闭
- c# - 提高将字节转换为 UInt32 的性能
- amazon-web-services - aws负载均衡器http到https重定向不起作用
- ruby - 从 Ruby 2.7 升级到 3.0 时出现 ArgumentError - 与位置参数和关键字参数的分离有关?
- node.js - DocumentError“权限'documentai.processors.processOnline'在资源'我的处理器'上被拒绝(或者它可能不存在)
- c++ - “C 中的可选原型设计而 C++ 中的强制原型设计”是什么意思?