我一直致力於整合PhantomJS以幫助在我的服務器上創建圖像,然後可以在HTML模板中使用該圖像並使用mPDF將其保存爲PDF。我正在使用Yii2,並設法使用一些僞腳本生成並保存圖像。但我不確定如何爲動態數據做到這一點。與PHP循環一起使用PhantomJs
目前我有這種非常粗略的實現:
shell.php
if(exec('C:\xampp\htdocs\phantomjs\bin\phantomjs C:\xampp\htdocs\ reporting\views\site\test.php')){
echo 'yes';
} else echo 'no';
我test.php
文件基本上是用PHP擴展的JavaScript文件。
test.php的
var system = require('system');
var page = require('webpage').create();
var fs = require('fs');
// load JS libraries
if(page.injectJs('C:\\xampp\\htdocs\\phantomjs\\jquery.1.9.1.min.js')){
console.log('Jquery used');
}
if(page.injectJs('C:\\xampp\\htdocs\\phantomjs\\highcharts.js')){
console.log('highcharts used');
}
if(page.injectJs('C:\\xampp\\htdocs\\phantomjs\\highcharts-convert.js')){
console.log('highcharts-convert used');
}
var args = {
width: 600,
height: 500
};
var svg = page.evaluate(function(opt){
$('body').prepend('<div id="container"></div>');
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
width: opt.width,
height: opt.height
},
exporting: {
enabled: false
},
title: {
text: 'Combination chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Bananas', 'Plums']
},
yAxis: {
title: {
text: 'Y-values'
}
},
labels: {
items: [{
html: 'Total fruit consumption',
style: {
left: '40px',
top: '8px',
color: 'black'
}
}]
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
},
series: {
enableMouseTracking: false,
shadow: false,
animation: false
}
},
series: [{
type: 'column',
name: 'Andrii',
data: [3, 2, 1, 3, 4]
}, {
type: 'column',
name: 'Fabian',
data: [2, 3, 5, 7, 6]
}, {
type: 'column',
name: 'Joan',
data: [4, 3, 3, 9, 0]
}, {
type: 'spline',
name: 'Average',
data: [3, 2.67, 3, 6.33, 3.33],
marker: {
lineWidth: 2,
lineColor: 'white'
}
}, {
type: 'pie',
name: 'Total consumption',
data: [{
name: 'Andrii',
y: 13,
color: '#4572A7'
}, {
name: 'Fabian',
y: 23,
color: '#AA4643'
}, {
name: 'Joan',
y: 19,
color: '#89A54E'
}],
center: [100, 80],
size: 100,
showInLegend: false,
dataLabels: {
enabled: false
}
}]
});
return chart.getSVG();
}, args);
// Saving SVG to a file
fs.write("test.svg", svg);
// Saving diagram as PDF
//setTimeout(function(){
page.render('test.png', {format: 'png', quality: '100'});
phantom.exit();
這產生並保存png
文件(雖然尺寸未設定)。
我已經認識到,但我不能在這個文件中使用PHP。 exec()
調用永遠不會返回,我只是最終取消它。我假設這是因爲PhantomJS在遇到<?php
標籤時不知道該怎麼做。
什麼是嘗試使用PHP來生成和保存圖表循環數據庫數據的最佳方式?
你有沒有嘗試過像返回1;而不是phantom.exit(); –
@thomasw_lrd我沒有沒有。它在幻影文檔中說始終使用exit()。 – Jonnny