【发布时间】: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