Wednesday 17 May 2017

Nightmare.js does not work with Azure webjob

I am trying to run an azure webjob which takes a json object and renders a webpage, then prints it to pdf, via the electron browser via Nightmare.js.

When I run this locally it works perfectly, but when I run it in azure webjob it never completes.
I get the two console.log statements output to the log, but seeing as I can not output anything from the nightmare.js calls, nor display the electron browser window, I have no idea what is going wrong.

There is also a webserver in the script, omitted as it seems to take the request with the json object and pass it to creatPage just fine.

I have verified that index.html file is in the right directory. Does anyone know what might be wrong?

var Nightmare = require('nightmare'),
    http = require('http');

function createPage(o, final) {

    var start = new Date().getTime();
    var page = Nightmare({
        //show: true, //uncomment to show electron browser window
        //openDevTools: { mode: 'detach'}, //uncomment to open developer console ('show: true' needs to be set)
        gotoTimeout: 300000, //set timeout for .goto() to 2 minutes
        waitTimeout: 300000, //set timeout for .wait() to 5 minutes
        executionTimeout: 600000 //set timeout for .evaluate() to 10 minutes
    })
    .goto('file:\\\\' + __dirname + '\\index.html');

    page.wait("#ext-quicktips-tip") //wait till HTML is loaded
    .wait(function () { // wait till JS is loaded
        console.log('Extjs loaded.');
        return !!(Ext.isReady && window.App && App.app);
    });

    console.log("CreatePage()1");

    page.evaluate(function (template, form, lists, printOptions) {
        App.pdf.Builder.create({
            template: template,
            form: form,
            lists: lists,
            format: o.printOptions.format,
        });
        console.log('Create done');
    }, template, form, o.lists, printOptions);

    console.log("CreatePage()2");
    page.wait(function () {
        console.log('Content created. ' + App.pdf.Builder.ready);
        return App.pdf.Builder.ready;
    })
    .pdf(o.outputDir + form.filename, { "pageSize": "A4", "marginsType": 1 })
    .end()
    .then(function () {
        console.log('Pdf printed, time: ' + (new Date().getTime() - start) / 1000 + ' seconds');
        final(true);
    })
    .catch(function (err) {
        console.log('Print Error: ' + err.message);
    });
}



via Jeppe

No comments:

Post a Comment