I've started using Phantom JS on Windows, but I'm having a bit of difficulty finding documentation on its capability (probably the root of my problem).
Using Phantom JS I would like to do the following:
I'm sure this is probably possible, but I wasn't able to find the Phantom JS function call for:
Converting HTML to JPG with the Windows screen capture toolOpen your HTML file in your browser or any viewable tool. Take a snapshot of an area with your screen capture tool (Snipping tool on Windows, for example). Click File > Save as. Select the location and select the Save as type as JPG.
Linux 64 bitsudo mv $PHANTOM_JS /usr/local/share sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin. Execute phantomjs –v at the terminal and it should give the version of PhantomJS.
var page = require('webpage').create(), loadInProgress = false, fs = require('fs');
var htmlFiles = new Array();
console.log(fs.workingDirectory);
var curdir = fs.list(fs.workingDirectory);
// loop through files and folders
for(var i = 0; i< curdir.length; i++)
{
var fullpath = fs.workingDirectory + fs.separator + curdir[i];
// check if item is a file
if(fs.isFile(fullpath))
{
// check that file is html
if(fullpath.indexOf('.html') != -1)
{
// show full path of file
console.log('File path: ' + fullpath);
htmlFiles.push(fullpath);
}
}
}
console.log('Number of Html Files: ' + htmlFiles.length);
// output pages as PNG
var pageindex = 0;
var interval = setInterval(function() {
if (!loadInProgress && pageindex < htmlFiles.length) {
console.log("image " + (pageindex + 1));
page.open(htmlFiles[pageindex]);
}
if (pageindex == htmlFiles.length) {
console.log("image render complete!");
phantom.exit();
}
}, 250);
page.onLoadStarted = function() {
loadInProgress = true;
console.log('page ' + (pageindex + 1) + ' load started');
};
page.onLoadFinished = function() {
loadInProgress = false;
page.render("images/output" + (pageindex + 1) + ".png");
console.log('page ' + (pageindex + 1) + ' load finished');
pageindex++;
}
Hope this helps. For more information about the FileSystem calls, check this page out: http://phantomjs.org/api/fs/
Also, I wanted to add, that I believe the FileSystem functions are only available in PhantomJS 1.3 or later. Please make sure to run the latest version. I used PyPhantomJS for Windows but I'm sure this will work without a hitch on other systems as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With