首页 > 解决方案 > 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]
        ]
    ]
]
]);
?>

标签: yii2chart.jsmpdf

解决方案


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://phantomjs.org/

http://jonnnnyw.github.io/php-phantomjs/

for PDF: http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/#output-to-pdf


推荐阅读