【发布时间】:2019-03-10 14:19:22
【问题描述】:
我正在使用Chart.js。
这是我的 php 文件,我使用了 javascript,通过它我只是创建一个图表并在此之后创建图表图像。
<script>
function done(){
var url=myLine.toBase64Image();
document.getElementById("url").src=url;
}
var MONTHS = [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>];
var config = {
type: 'line',
data: {
labels: [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>],
datasets: [{
label: "<?php echo $asx_code ?>",
backgroundColor: '#ff6384',
borderColor:'#ff6384',
data: [<?php echo '"' . implode('","', array_reverse($close_price_arr)) . '"' ?>],
fill: false,
}]
},
options: {
bezierCurve : false,
animation: {
onComplete: done
},
responsive: true,
title: {
display: true,
text: ''
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
}
}
};
var ctx = document.getElementById('canvascode').getContext('2d');
window.myLine = new Chart(ctx, config);
document.getElementById('randomizeData').addEventListener('click', function() {
config.data.datasets.forEach(function(dataset) {
dataset.data = dataset.data.map(function() {
return randomScalingFactor();
});
});
window.myLine.update();
});
var colorNames = Object.keys(window.chartColors);
</script>
这工作得很好,但现在我被困在 html2pdf 做同样的事情。我想包含几个JS 文件并使用script 标签生成我的图表,然后将图像放入我的PDF 文件中。
这是我在没有任何运气的情况下所做的。
<?php
require __DIR__.'/vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
$html2pdf = new Html2Pdf();
$html2pdf->pdf->IncludeJS('Chart.bundle.js');
$html2pdf->pdf->IncludeJS('chart_utils.js');
$asx_code = 'CHK';
$url = 'https://www.asx.com.au/asx/1/share/' . $asx_code . '/prices?interval=daily&count=365';
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$record_decode = json_decode($result);
$close_date_arr = array();
$close_price_arr = array();
foreach ($record_decode->data as $current_data) {
$close_price_arr[] = $current_data->close_price;
$close_date_arr[] = date('F Y', strtotime($current_data->close_date));
}
$str = '<canvas id="canvascode" style="display:none"></canvas>
<img id="url" style="width:400px; height:400px" />';
?>
<script>
function done(){
var url=myLine.toBase64Image();
document.getElementById("url").src=url;
}
var MONTHS = [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>];
var config = {
type: 'line',
data: {
labels: [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>],
datasets: [{
label: "<?php echo $asx_code ?>",
backgroundColor: '#ff6384',
borderColor:'#ff6384',
data: [<?php echo '"' . implode('","', array_reverse($close_price_arr)) . '"' ?>],
fill: false,
}]
},
options: {
bezierCurve : false,
animation: {
onComplete: done
},
responsive: true,
title: {
display: true,
text: ''
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
}
}
};
var ctx = document.getElementById('canvascode').getContext('2d');
window.myLine = new Chart(ctx, config);
document.getElementById('randomizeData').addEventListener('click', function() {
config.data.datasets.forEach(function(dataset) {
dataset.data = dataset.data.map(function() {
return randomScalingFactor();
});
});
window.myLine.update();
});
var colorNames = Object.keys(window.chartColors);
</script>
<?php
$html2pdf->writeHTML($str);
$html2pdf->output();
?>
当我尝试生成 PDF 文件时,我收到一条错误消息,
致命错误:未捕获的 Spipu\Html2Pdf\Exception\HtmlParsingException:Html2Pdf 不知道 html 标签 [canvas]。您可以创建它并将其推送到 Html2Pdf GitHub 项目上。在 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php:1435 堆栈跟踪:#0 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1043 ): Spipu\Html2Pdf\Html2Pdf->_executeAction(Object(Spipu\Html2Pdf\Parsing\Node)) #1 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(749): Spipu\ Html2Pdf\Html2Pdf->_setNewPositionForNewLine(NULL) #2 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1415): Spipu\Html2Pdf\Html2Pdf->_setNewPage() #3 /var/ www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1401): Spipu\Html2Pdf\Html2Pdf->_executeAction(Object(Spipu\Html2Pdf\Parsing\Node)) #4 /var/www/html/ htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(595): Spipu\Html2Pdf\Html2Pdf->_makeHTMLcode() #5 /var/www/html/htmlpdf/test.php(138): Spipu\Html2Pdf\Html2Pdf ->写HTML('
谁能指导我如何在这里生成我的脚本并只将我的图像放入 PDF 中。
【问题讨论】:
-
这不适用于任何 javascript 图表库,因为 HTML2PDF 不会执行脚本来生成图表。最简单的方法是使用 PHP 图表库生成图表。否则你可能会使用 node.js 来运行你的 javascript 服务器端。