简体   繁体   中英

Using wkhtmltopdf to output Google Charts to PDF

We have a Silverstripe project that uses the silverstripe-wkhtmltopdf module to output HTML/CSS/Javascript as PDF.

Simple Javascript like document.write works but I want to output Google Charts using their visualisation API:

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

The PDF wasn't showing any of the visualisation output so I used QTBrowser to debug the Javascript - as suggested here: Debugging javascript in wkhtmltopdf

The error I'm getting in QTBrowser is: 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

The HTML looks correct at my end but I don't know the compatibility of QTBrowser, or how it relates to wkhtmltopdf.

Has anyone had any experience/ success with using wkhtmltopdf to output Google Charts?

Here's a good post which explains this topic and shows you how to achieve it

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM