使用wkhtmltopdf将Google Charts输出为PDF

时间:2016-10-10 01:50:10

标签: javascript silverstripe wkhtmltopdf phpwkhtmltopdf

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

像document.write这样的简单Javascript可以工作,但我想使用他们的可视化API输出Google Charts:

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

PDF没有显示任何可视化输出,因此我使用QTBrowser来调试Javascript - 如下所示:Debugging javascript in wkhtmltopdf

我在QTBrowser中遇到的错误是Error: one or more fonts could not be loaded.来自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输出Google Charts有经验/成功?

1 个答案:

答案 0 :(得分:2)

这是一个很好的帖子,它解释了这个主题并向您展示了如何实现它

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>