Reputation: 5111
I have been struggling against a really very weird scenario in PHP. I have a main script which creates some HTML (including a google graph). But it has some <SCRIPT>
tags that won't be rendered by MPDF library. So here's what I did:
Created a new file g_chart.php
and included all the graph generating code in it as follows:
<meta charset="UTF-8">
<script>
var save_as_file = '<?php echo $_GET['file']; ?>';
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>
<script>
google.load('visualization', '1', {packages: ['corechart', 'bar']});
google.setOnLoadCallback(drawBasic);
function drawBasic() {
var data = google.visualization.arrayToDataTable([
['City', '2010 Population',],
['حواس', 8175000],
['انسانی جسم کے حصوں کے نام اور افعال', 3792000],
['Chicago, IL', 2695000],
['Houston, TX', 2099000],
['صوتیات : ہجے اور الفاظ', 1526000]
]);
var options = {
title: 'Population of Largest U.S. Cities',
chartArea: {width: '50%'},
hAxis: {
title: 'Total Population',
minValue: 0
},
vAxis: {
title: 'City'
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'ready', function () {
chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
});
chart.draw(data, options);
var img_src = $('#chart_div img').attr('src');
$('#chart_div').after('<form name="frm_img"><input type="hidden" name="img_src" value="'+img_src+'"></form>');
SaveToDisk();
}
function SaveToDisk() {
var oform = $('form[name="frm_img"]');
var datastring = oform.serialize();
$.ajax({
type : "POST",
url : "g_chart_save.php?file="+save_as_file,
data : datastring,
cache : false,
async : true,
success : function(html){
}
});
}
</script>
<div id="chart_div"></div>
g_chart_save.php file is as follows:
<?php
if (!empty($_POST)) {
$img_src = $_POST['img_src'];
$file = $_GET['file'];
$image = base64_to_jpeg( $img_src, $file.'.png' );
}
function base64_to_jpeg($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
?>
So basically, when I call g_chart.php?file=graph
in the browser. My graph is generated and saved to same directory as a physical graph.png
file.
By creating above work-around, I now have to call this graph generating script g_chart.php?file=graph
from my main script's PHP file (while it is being executed i.e. runtime).
I have used following methods to make it happen:
Everytime I use any of the above methods, it hits the target file g_chart.php
but it actually doesn't execute it rather it returns me the contents or code that I have written in target file.
I want it to be executed SILENTLY, so that my graph's image file is created and I can then include it in my PDF.
Remember, I will be passing my graph data to target file in a GET request later, so as to make it dynamic.
Any help would be much appreciated.
Upvotes: 1
Views: 345
Reputation: 36
Since the process you describe relies on client side Javascript to be executed this approach won't work. Curl and the other various ways of fething the document over http will only do just that.
However, you might be able to use The PhantomJS headless Webkit scriptable to achieve the desired result. Using it you could probably script it to extract the graph image without posting it back to the webserver using PHP.
Upvotes: 1