2015-03-27 11:54:44 +00:00
|
|
|
#!/usr/bin/env phantomjs
|
|
|
|
|
|
|
|
var system = require('system');
|
|
|
|
|
2015-03-29 14:15:05 +00:00
|
|
|
if (system.args.length < 6) {
|
|
|
|
console.log("usage: " + system.args[0] + " <url> <filename> <format> <selector> <scale>");
|
|
|
|
console.log(" ex: " + system.args[0] + " page.html page.png png 'body > div' 2");
|
2015-03-27 11:54:44 +00:00
|
|
|
phantom.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = system.args[1];
|
|
|
|
var filename = system.args[2];
|
2015-03-29 14:15:05 +00:00
|
|
|
var format = system.args[3];
|
|
|
|
var selector = system.args[4];
|
|
|
|
var scale = parseInt(system.args[5], 10);
|
|
|
|
|
2015-03-27 11:54:44 +00:00
|
|
|
var page = require('webpage').create();
|
|
|
|
|
|
|
|
page.viewportSize = { width: 9999, height: 9999 };
|
2015-03-29 14:15:05 +00:00
|
|
|
page.zoomFactor = scale;
|
|
|
|
|
|
|
|
page.onConsoleMessage = function(msg) {
|
|
|
|
console.log('console.log: ' + msg);
|
|
|
|
};
|
2015-03-27 11:54:44 +00:00
|
|
|
|
|
|
|
page.onError = function(msg, trace) {
|
|
|
|
console.log('Script error: ' + msg);
|
|
|
|
phantom.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
page.onResourceError = function(resourceError) {
|
|
|
|
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
|
|
|
|
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
|
|
|
|
phantom.exit(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
page.open(url, function(status) {
|
|
|
|
if (status !== "success") {
|
|
|
|
console.log("Failed to load " + url);
|
|
|
|
phantom.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:15:05 +00:00
|
|
|
var rect = page.evaluate(function(selector) {
|
2015-03-27 11:54:44 +00:00
|
|
|
// Alternative to zoomFactor:
|
|
|
|
//
|
|
|
|
// /* scale the whole body */
|
|
|
|
// document.body.style.webkitTransform = "scale(2)";
|
|
|
|
// document.body.style.webkitTransformOrigin = "0% 0%";
|
|
|
|
// /* fix the body width that overflows out of the viewport */
|
|
|
|
// document.body.style.width = "50%";
|
|
|
|
//
|
|
|
|
// It has small issue with letter spacing though.
|
|
|
|
|
2015-03-29 14:15:05 +00:00
|
|
|
var elements = document.querySelectorAll(selector);
|
2015-03-27 11:54:44 +00:00
|
|
|
|
2015-03-29 14:15:05 +00:00
|
|
|
if (elements.length > 0) {
|
|
|
|
return elements[0].getBoundingClientRect();
|
|
|
|
}
|
|
|
|
}, selector);
|
|
|
|
|
|
|
|
if (!rect) {
|
|
|
|
console.log("Couldn't get geometry of requested DOM element");
|
2015-03-27 11:54:44 +00:00
|
|
|
phantom.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-29 14:15:05 +00:00
|
|
|
page.clipRect = {
|
|
|
|
left: rect.left * scale,
|
|
|
|
top: rect.top * scale,
|
|
|
|
width: rect.width * scale,
|
|
|
|
height: rect.height * scale
|
|
|
|
};
|
|
|
|
|
|
|
|
page.render(filename, { format: format });
|
2015-03-27 11:54:44 +00:00
|
|
|
phantom.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// vim: ft=javascript
|