使用wkhtmltopdf将谷歌图表输出为PDF

Using wkhtmltopdf to output Google Charts to PDF

本文关键字:输出 PDF wkhtmltopdf 谷歌 使用      更新时间:2023-09-26

我们有一个Silverstripe项目,它使用Silverstripe -wkhtmltopdf模块将HTML/CSS/Javascript输出为PDF。

简单的Javascript类文档。写作品,但我想输出谷歌图表使用他们的可视化API:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
  google.load('visualization', '1', {packages: ['corechart', 'bar']});
</script>

PDF没有显示任何可视化输出,所以我使用QTBrowser来调试Javascript -如下所示:

我在QTBrowser中得到的错误是:Error: one or more fonts could not be loaded. from https://www.google.com/uds/api/visualization/1.0/b5ac9efed10eef460d14e653d05f5fbf/webfontloader,dygraph,format+en,default+en,ui+en,bar+en,corechart+en.I.js:737

HTML看起来是正确的,但我不知道QTBrowser的兼容性,或者它与wkhtmltopdf的关系。

有没有人有任何经验/成功使用wkhtmltopdf输出谷歌图表?

这里有一篇很好的文章,它解释了这个主题,并告诉你如何实现它

http://codingexplained.com/coding/php/using-google-visualization-with-wkhtmltopdf

    <script type="text/javascript">
        function init() {
            google.load("visualization", "1.1", { packages:["corechart"], callback: 'drawCharts' });
        }
        function drawCharts() {
            drawAccountImpressions('chart-account-impressions');
        }
        function drawAccountImpressions(containerId) {
            var data = google.visualization.arrayToDataTable([
                ['Day', 'This month', 'Last month'],
                ['01', 1000, 400],
                ['05', 800, 700],
                ['09', 1000, 700],
                ['13', 1000, 400],
                ['17', 660, 550],
                ['21', 660, 500],
                ['23', 750, 700],
                ['27', 800, 900]
            ]);
            var options = {
                width: 700,
                height: 400,
                hAxis: { title: 'Day',  titleTextStyle: { color: '#333' } },
                vAxis: { minValue: 0 },
                curveType: 'function',
                chartArea: {
                    top: 30,
                    left: 50,
                    height: '70%',
                    width: '100%'
                },
                legend: 'bottom'
            };
            var chart = new google.visualization.LineChart(document.getElementById(containerId));
            chart.draw(data, options);
        }
    </script>
</head>
<body onload="init()">
    <div id="chart-account-impressions"></div>
</body>