我正在尝试运行一个azure webjob,它接受一个json对象并渲染一个网页,然后通过Nightmare.js中的电子浏览器将其打印为pdf。
当我在本地运行它时,它工作得很好,但是当我在 azure webjob 中运行它时,它永远不会完成。
我将两个 console.log
语句输出到日志中,但由于我无法从 nights.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
它有以下与我的问题相关的段落:
PDF generation from HTML
There are multiple libraries used to convert HTML to PDF. Many Windows/.NET specific versions leverage IE APIs and therefore leverage User32/GDI32 extensively. These APIs are largely blocked in the sandbox (regardless of plan) and therefore these frameworks do not work in the sandbox.
There are some frameworks that do not leverage User32/GDI32 extensively (wkhtmltopdf, for example) and we are working on enabling these in Basic+ the same way we enabled SQL Reporting.
最佳答案
我猜想,要使 Nightmare.js 正常工作,您需要桌面交互,而 WebJob 上无法提供这种交互。
取自 this issue在 Github 上:
Nightmare isn't truly headless: it requires an Electron instance to work, which in turn requires a framebuffer to render properly (at least, for now).
这不会在 Azure WebJob 上运行。
关于javascript - Nightmare.js 不适用于 Azure Webjob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44037918/