src/hdalab/scripts/capture-phantomjs.js
changeset 492 19220d52bce7
equal deleted inserted replaced
491:8444b4f67e45 492:19220d52bce7
       
     1 //from https://github.com/makinacorpus/django-screamshot
       
     2 
       
     3 var page = require('webpage').create(),
       
     4     system = require('system');
       
     5 
       
     6 /** 
       
     7  * arguments:
       
     8  * [1] => URL
       
     9  * [2] => output. Use /dev/stdout if you want to capture.
       
    10  * [3] => size
       
    11  */
       
    12 
       
    13 var address = system.args[1],
       
    14     output  = system.args[2];
       
    15 
       
    16 config = {}
       
    17 system.args.forEach(function(arg, i) {
       
    18     if (i > 2) {
       
    19         namev = arg.split('=');
       
    20         config[namev[0].replace('--', '')] = namev[1];
       
    21     }
       
    22 });
       
    23 
       
    24 var method = config.method || 'get',
       
    25      width = config.width || 1400,
       
    26     height = config.height || 1,
       
    27       wait = config.wait || 200;
       
    28 
       
    29 /**
       
    30  * please note: the default height is intentionaly left as 1.
       
    31  * the thing is, if you skip the height, phantomjs falls back
       
    32  * to some default viewport. and that's not what we want. we
       
    33  * want to set the width, and let the height auto-calculate.
       
    34  */
       
    35 page.viewportSize = {width: width, height: height}
       
    36 
       
    37 format = config.format || 'png';
       
    38 
       
    39 if (format == 'pdf') {
       
    40     page.paperSize = {
       
    41         format: 'A4',
       
    42         orientation: config.orientation || 'portrait',
       
    43         margin: config.margin || '0cm'
       
    44     }
       
    45 }
       
    46 
       
    47 page.settings.resourceTimeout = 10000;
       
    48 
       
    49 page.open(address, function(status){
       
    50     if (status == 'success') {
       
    51         setTimeout(function(){
       
    52             page.render(output, {format: format});
       
    53             phantom.exit();
       
    54         }, wait);
       
    55     }
       
    56 });