简体   繁体   English

是否可以使用dompdf导出Google Chart?

[英]Is it possible to export a Google Chart using dompdf?

In my Laravel 5 project, I am using Lavacharts (powered by Google Charts) for displaying charts and a dompdf wrapper, laravel-dompdf , for generating PDF files. 在我的Laravel 5项目中,我使用Lavacharts (由Google Charts提供支持)来显示图表,使用dompdf包装器laravel-dompdf来生成PDF文件。

Is it possible to export such charts using dompdf, and if so, how can this be achieved? 是否可以使用dompdf导出此类图表,如果是这样,如何实现?

Presumably I would have to first save the chart as an image, but this is not really an option since saving the image is done via Javascript and for generating the PDF all the work is done in the backend (PHP). 据推测,我必须首先将图表保存为图像,但这不是一个真正的选项,因为通过Javascript保存图像并生成PDF所有工作都在后端(PHP)完成。

Dompdf is unable to process JavaScript-based content on a page. Dompdf无法在页面上处理基于JavaScript的内容。 You have two options: 您有两种选择:

  1. Preview the charts in a browser and send an image version to the server for rendering. 在浏览器中预览图表并将图像版本发送到服务器进行渲染。
  2. Use a headless browser (like PhantomJS ) to render your HTML (with JavaScript) to PDF 使用无头浏览器(如PhantomJS )将HTML(使用JavaScript)呈现为PDF

I don't have much to say on the second option, but on the first... 我对第二种选择没什么好说的,但是第一种选择......

The Google Charts API can generate a PNG version of your chart. Google Charts API可以生成图表的PNG版本 There are plenty of posts on SO on how to get the PNG to your PHP code ( search it ). 关于如何将PNG发送到PHP代码( 搜索它 ),有很多关于SO的帖子。

Something like the following might work: 像下面这样的东西可能会起作用:

<script type="text/javascript">
google.charts.load("current", {packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {

  var data = google.visualization.arrayToDataTable([
    ['Element', 'Density', { role: 'style' }],
    ['Copper', 8.94, '#b87333', ],
    ['Silver', 10.49, 'silver'],
    ['Gold', 19.30, 'gold'],
    ['Platinum', 21.45, 'color: #e5e4e2' ]
  ]);

  var options = {
    title: "Density of Precious Metals, in g/cm^3",
    bar: {groupWidth: '95%'},
    legend: 'none',
  };

  var chart_div = document.getElementById('chart_div');
  var chart_input = document.getElementById('chart_input');
  var chart = new google.visualization.ColumnChart(chart_div);

  // Wait for the chart to finish drawing before calling the getImageURI() method.
  google.visualization.events.addListener(chart, 'ready', function () {
    chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
    chart_input.value = chart.getImageURI();
  });

  chart.draw(data, options);

}
</script>

<div id='chart_div'></div>

<form method="post" action="print_chart.php">
<input type="hidden" name="chartImg" id="chartImg">
<button type="submit">print</button>
</form>

Google Charts renders the PNG using a data-uri, which dompdf supports. Google Charts使用dompdf支持的数据uri呈现PNG。 Once you have the PNG on the server side you can just insert it into an img tag: 在服务器端获得PNG后,您只需将其插入到img标记中即可:

$html = '<img src="' . $_POST['chart_input'] . '">';
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('chart.pdf');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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