I had a fairly simple page reporting some figures with a couple of graphs on the screen. I figured as TCPDF supports SVG and the graphs are generated in SVG, that would be the best format to capture them in, surprisingly I couldn't find much about doing so, so here's a generic version of my script for phantomjs:
var dest_folder = 'C:\\myfolder\\subfolder\\';
console.log('Destination Folder: ' + dest_folder);
var page = require('webpage').create();
page.open('http://www.example.com/graphs_page/', function(status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
console.log('Loaded Page');
// Get the first graph
graph_1_svg = page.evaluate(function() {
return document.getElementById('results_graph_1').children[0].innerHTML;
});
console.log('graph_1_svg: ' + graph_1_svg.length + ' chars long');
// Write to destination folder or report error to console
var fs = require('fs');
try {
fs.write(dest_folder + 'graph_1.svg', graph_1_svg, 'w');
} catch(e) {
console.log(e);
}
// Get the second graph
graph_2_svg = page.evaluate(function() {
return document.getElementById('results_graph_2').children[0].innerHTML;
});
console.log('graph_2_svg: ' + graph_2_svg.length + ' chars long');
// Write to destination folder or report error to console
var fs = require('fs');
try {
fs.write(dest_folder + 'graph_2.svg', graph_2_svg, 'w');
} catch(e) {
console.log(e);
}
phantom.exit();
}
});
The page features two target divs for highcharts, results_graph_1 and results_graph_2 and highcharts creates a child div within, then the svg within that, so by reaching in to the contents of each target's first child div we can copy out the svg and write it out to a file on the server.
After that pick up the SVG files to put in your PDF. I'm not sure what your situation is but I intend users to view the report on-screen before clicking to download the PDF, so I'll have that first stage save the graphs and the SVG files will be ready before the PDF is generated.
As far as running phantomjs goes if you download it, save this script in there as test.js, then in the command prompt go to where you extracted it, say c:\phantomjs and run:
phantomjs.exe test.js
The console.log parts of the script will notify you of its progress. Oh and change the destination folder & target web address in the script before you run it.