【问题标题】:Nightmare.js does not work with Azure webjobNightmare.js 不适用于 Azure 网络作业
【发布时间】:2017-10-17 16:29:50
【问题描述】:

我正在尝试运行一个 azure webjob,它接受一个 json 对象并呈现一个网页,然后通过 Nightmare.js 中的电子浏览器将其打印为 pdf。

当我在本地运行它时,它运行良好,但是当我在 azure webjob 中运行它时,它永远不会完成。
我将两个console.log 语句输出到日志中,但是看到我无法从 nightmare.js 调用中输出任何内容,也无法显示电子浏览器窗口,我不知道出了什么问题。

脚本中还有一个网络服务器,省略了,因为它似乎接受带有 json 对象的请求并将其传递给 createPage 就好了。

我已验证 index.html 文件位于正确的目录中。有谁知道可能出了什么问题?

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);
    });
}

已解决

正如 Rick 在他的回答中所说,这目前不起作用! 本文档列出了 webjobs 沙箱的当前状态:
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox
它有以下与我的问题有关的段落:

从 HTML 生成 PDF

有多个库用于将 HTML 转换为 PDF。许多 Windows/.NET 特定版本利用 IE API,因此广泛利用 User32/GDI32。这些 API 在沙箱中大部分被阻止(无论计划如何),因此这些框架在沙箱中不起作用。

有些框架没有广泛利用 User32/GDI32(例如 wkhtmltopdf),我们正在努力在 Basic+ 中启用这些框架,就像启用 SQL 报告一样。

【问题讨论】:

  • 您使用的是哪种类型的WebJobs?按需、连续还是按计划?
  • 我一直在使用,因为我也在脚本中运行 http 网络服务器。
  • 函数createPage在哪里调用?
  • 从http服务器调用。当它收到一个 POST 请求时,它会使用一个 json 对象 o 和一个回调 final 调用 createPage

标签: javascript node.js azure azure-webjobs nightmare


【解决方案1】:

我猜要让 nightmare.js 工作,你需要桌面交互,而你在 WebJob 上没有。

取自 Github 上的this issue

Nightmare 并不是真正的无头:它需要一个 Electron 实例 工作,这反过来又需要一个帧缓冲区来正确渲染(在 至少,现在)。

这不会在 Azure WebJob 上运行。

【讨论】:

  • 非常感谢,让我搜索了正确的关键字
  • 您好,我有一个使用 Nightmare 进行 Web 报废的 API,并且在 Azure CI 期间测试失败...您找到了实现此目的的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
相关资源
最近更新 更多