Skip to content

Instantly share code, notes, and snippets.

@pantaluna
Created September 28, 2016 21:14
Show Gist options
  • Save pantaluna/f3b38bea0630bbd34be5e41b35d8968f to your computer and use it in GitHub Desktop.
Save pantaluna/f3b38bea0630bbd34be5e41b35d8968f to your computer and use it in GitHub Desktop.
SlimerJS page.papersize = Object
#CYGWIN>>
cd /cygdrive/c/myhtdocs/fdb-calendar-feature/calendar-image-generator
export SLIMERJSLAUNCHER="/cygdrive/c/Program Files (x86)/Mozilla Firefox/firefox.exe"
rm --verbose output-papersize*
slimerjs-0.10.0/slimerjs -debug yes dev-slimerjs-papersize.js 3507 2481 true output-papersize http://www.feestdagen-belgie.be/kalender
/*
* @doc 300DPI - A4 Landscape - w 11.69" h 8.27" => width 3507 px - height 2481 px
*/
/*
* Modules
* @req Sequence matters!
*/
const system = require('system');
const args = system.args;
let webPage = require('webpage');
/*
* Constants
*/
const DELAY_RENDER_SECONDS = 3000; // @doc 3 seconds
// @doc Google Chrome V53
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36';
/*
* Helper Funcs
*/
function dumpArguments() {
console.log('#Arguments: ' + args.length);
args.forEach(function (arg, i) {
console.log(i + ': ' + arg);
});
}
function dumpToConsole(title, data) {
console.log(`---${title || ''} (${typeof data})---: `);
console.log(data);
}
function consoleLog(data) {
console.log(`${new Date().toUTCString()} ${data}`);
}
function consoleError(data) {
console.error(`${new Date().toUTCString()} ${data}`);
}
/*
* Args
*/
dumpArguments();
let nWidth = Number(system.args[1]) || 1366;
let nHeight = Number(system.args[2]) || 768;
let argClipped = system.args[3] || 'false';
let bClipped = (argClipped === 'true');
let sOutputBaseFileName = system.args[4] || 'screenshot';
let sUrl = system.args[5] || 'http://www.cnn.com/';
dumpToConsole('width', nWidth);
dumpToConsole('height', nHeight);
dumpToConsole('clipped', bClipped);
dumpToConsole('sImageBaseFileName', sOutputBaseFileName);
dumpToConsole('url', sUrl);
/*
* Variables
*/
/*
* MAIN
*/
//noinspection JSCheckFunctionSignatures
let page = webPage.create();
page.onLongRunningScript = function (message) {
dumpToConsole('message', message);
page.stopJavaScript();
};
page.viewportSize = {
width: nWidth,
height: nHeight
};
page.settings.userAgent = USER_AGENT;
if (bClipped) {
consoleLog('Clipping the output.');
page.clipRect = {
top: 0,
left: 0,
width: nWidth,
height: nHeight
};
}
consoleLog('PDF page.paperSize()');
/////page.paperSize({format: 'A4',orientation: 'landscape'});
page.paperSize = {format: 'A4',orientation: 'landscape'};
consoleLog('Opening webpage.');
page.open(sUrl)
.then(
function (status) {
consoleLog('page.open() promise Status: ' + status);
if (status !== 'success') {
consoleError('Cannot open the web page (func status <> success)');
}
page.zoomFactor = 2;
consoleLog('Waiting ' + DELAY_RENDER_SECONDS / 1000 + ' seconds (let the zoomFactor & JS do its work).');
window.setTimeout(function () {
consoleLog('Taking screenshot (PDF)');
consoleLog('PDF page.render()');
page.render(sOutputBaseFileName + '.pdf', {format: 'pdf'});
consoleLog('slimer.exit()');
slimer.exit();
}, DELAY_RENDER_SECONDS);
},
function (error) {
consoleError('.then(,reject): Cannot open the web page!');
consoleError(error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment