我正在尝试运行一个简单的 Azure 函数,该函数将根据浏览器中的可见内容进入页面并生成 PDF。
我创建了一个 NodeJS 12 Azure Function with Linux Consumption Plan (B1)。我设置PLAYWRIGHT_BROWSERS_PATH为0。
函数代码如下所示:
const { chromium } = require("playwright-chromium");
const baseUrl = "http://someurl";
const targetPage = "/action/";
module.exports = async function (context, req) {
const browser = await chromium.launch();
const page = await browser.newPage();
console.log (baseUrl + targetPage + context.bindingData.id)
await page.goto(baseUrl + targetPage + context.bindingData.id, { waitUntil: 'domcontentloaded' });
await page.emulateMedia({ media: 'print' });
onst result = await page.pdf({ printBackground: true, format: 'letter' });
context.res = {
body: result,
headers: { 'Content-Type': "application/pdf" }
};
await page.close();
await browser.close();
};
包.json
{
"name": "",
"version": "",
"scripts": {
"start": "func start",
"test": "echo \"No tests yet...\""
},
"description": "",
"devDependencies": {},
"dependencies": {
"playwright-chromium": "1.3.0"
}
}
和函数.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
],
"route": "route/{id}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
当我检查应用洞察时,我发现有以下错误:
Exception while executing function: Functions.PDF Result: Failure
Exception: Worker was unable to load function PDF: 'Error: Cannot find module 'playwright-chromium'
Require stack:
- /home/site/wwwroot/PDF/index.js
- /azure-functions-host/workers/node/worker-bundle.js
- /azure-functions-host/workers/node/dist/src/nodejsWorker.js'
Stack: Error: Cannot find module 'playwright-chromium'
噜噜哒
相关分类