使用 Cloudformatter 将 Google 图表导出为 PDF,生成空白 PDF

Export Google Chart to PDF using Cloudformatter, generates blank PDF

我正在尝试将 google 图表导出到 PDF 文件。我正在使用来自 cloudformatter.comxepOnline.jqPlugin.js 插件。我遵循了 http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage

中的所有步骤

但是当我尝试导出时,生成的 PDF 带有空白方框。

我正在从 jquery ajax 调用中加载 google 图表。任何人都可以告诉我可能是什么问题吗?

这是没有 ajax 调用的示例代码 -

 <button type="button" id="" class="btn btn-success pull-right" style="margin-right: 7px;" onclick="return xepOnline.Formatter.Format('chart_container', {render: 'download', filename: 'Years_Build', mimeType: 'application/pdf'});">Export</button>
    <div id="chart_container">
      <div id="chart_div"></div>
    </div>
<script type="text/javascript">
function drawChart() {
var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses', 'Profit'],
          ['2014', 1000, 400, 200],
          ['2015', 1170, 460, 250],
          ['2016', 660, 1120, 300],
          ['2017', 1030, 540, 350]
        ]);

        var options = {
          chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
          }
        };

        var chart = new google.charts.Bar(document.getElementById('chart_div'));
        chart.draw(data, google.charts.Bar.convertOptions(options));
                                                            }
google.charts.load('current', {'packages': ['bar']});
google.charts.setOnLoadCallback(drawChart);
</script>

您缺少需要添加的 SVG 命名空间。如果您在他们的网站上检查每个图表的示例 fiddle,您将看到一个在绘制图表后添加命名空间的函数。

例如,参见 http://jsfiddle.net/w1rpjxoe/

你会看到这个函数:

function AddNamespace(){
  var svg = jQuery('#chart_div svg');
  svg.attr("xmlns", "http://www.w3.org/2000/svg");
  svg.css('overflow','visible');
}

从这个 Listener 中调用:

 google.visualization.events.addListener(chart, 'ready', AddNamespace);

这样做是将缺少的 SVG 命名空间添加到图表 SVG 中,Google 图表不这样做,但@cloudformatter 需要。