2

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?

Community
  • 1
  • 1
BaronGrivet
  • 4,364
  • 5
  • 37
  • 52
  • Have you tried this solution http://stackoverflow.com/a/22465495 / http://codingexplained.com/coding/php/using-google-visualization-with-wkhtmltopdf ? And thanks for using my module ;) – csy_dot_io Oct 10 '16 at 18:51
  • @csy_dot_io - thank you for the excellent module! Your second link got me through - if you can post as answer I'll accept it: http://codingexplained.com/coding/php/using-google-visualization-with-wkhtmltopdf – BaronGrivet Oct 11 '16 at 21:58
  • you're wellcome. I've added it as an answer. later i'll extend it with an code example. – csy_dot_io Oct 12 '16 at 05:39

1 Answers1

3

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>

csy_dot_io
  • 1,199
  • 6
  • 19