如何在Azure云服务或Azure功能中运行Headless Chrome?

问题描述 投票:4回答:3

我正在尝试使用Headless Chrome从复杂的HTML文件生成PDF文件(包含图像,SVG等)。我能够在Cloud Service(Windows)上使用wkhtmltopdf.exe来生成简单的PDF文件,但我真的需要Chrome来生成尽可能接近HTML + SVG + Image的PDF。

我希望能够在Azure云服务或Azure功能中运行Headless Chrome,但我无法让它工作。我想这是由于对GDI的限制。我能够在我自己的机器上运行Azure模拟器中的代码和Headless Chrome,但是一旦部署它就无法工作。

下面是我目前在Azure Functions(适用于Windows)中运行的代码。我正在使用Puppeteer截取example.com的屏幕截图。如果我可以让它工作,我想生成PDF将变得容易。

const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const os = require('os');

module.exports = function (context, req) {
    function failureCallback(error) {
        context.log("--> Failure = '" + error + "'");
    }

    const chromeDir = path.normalize(__dirname + "/../node_modules/puppeteer/.local-chromium/win64-508693/chrome-win32/chrome.exe");
    context.log("--> Chrome Path = " + chromeDir);

    const dir = path.join(os.tmpdir(), '/screenshots');

    if (!fs.existsSync(dir)){
        fs.mkdirSync(dir);
    }

    const screenshotPath = path.join(dir, "example.png");
    context.log("--> Path = " + screenshotPath);

    let browser, page;
    puppeteer.launch({ executablePath: chromeDir, headless: true, args: [ '--no-sandbox', '--single-process', '--disable-gpu' ] })
        .then(b => {
            context.log("----> 1");
            browser = b;
            return browser.newPage();
        }, failureCallback)
        .then(p => {
            context.log("----> 2");
            page = p;
            return p.goto('https://www.example.com');
        }, failureCallback)
        .then(response => {
            context.log("----> 3");
            return page.screenshot({path: screenshotPath, fullPage: true});  
        }, failureCallback)
        .then(r => {
            browser.close();

            context.res = {
                body: "Done!"
            };

            context.done();            
        }, failureCallback);
};

以下是尝试执行脚本时的日志。

2017-12-18T04:32:05  Welcome, you are now connected to log-streaming service.
2017-12-18T04:33:05  No new trace in the past 1 min(s).
2017-12-18T04:33:11.400 Function started (Id=89b31468-8a5d-43cd-832f-b641216dffc0)
2017-12-18T04:33:20.578 JavaScript HTTP trigger function processed a request.
2017-12-18T04:33:20.578 --> Chrome Path D:\home\site\wwwroot\node_modules\puppeteer\.local-chromium\win64-508693\chrome-win32\chrome.exe
2017-12-18T04:33:20.578 --> Path = D:\local\Temp\screenshots\example.png
2017-12-18T04:33:20.965 --> Failure = 'Error: spawn UNKNOWN'
2017-12-18T04:33:20.965 ----> 2

错误“Failure ='Error:spawn UNKNOWN'”不清楚。我确保使用Kudu和PowerShell正确使用的路径是正确的。

我正在寻找一种在Azure云服务和/或Azure功能上运行Chrome的方法(对于Windows - 以便使用我现有的App Service计划)。有人还试图在Azure中运行Headless Chrome吗?我愿意接受任何有助于我使这个脚本工作的想法吗?

google-chrome azure azure-functions azure-cloud-services google-chrome-headless
3个回答
2
投票

我不确定Headless Chrome的使用情况,但由于某些GDI限制,Azure Functions运行的sandbox在从HTML生成PDF时遇到问题。

考虑在Azure Functions on Linux中尝试你的任务。虽然这仍然在预览中,但它没有使用沙盒,所以如果你可以使用无头镀铬工作,那么你可能会有更多的运气与PDF生成。


2
投票

我建议使用https://www.browserless.io/,这样您就不必在应用服务中运行chrome.exe。

用puppeteer.connect替换puppeteer.launch

const browser = await puppeteer.connect({
  browserWSEndpoint: 'wss://chrome.browserless.io/'
});

0
投票

Azure允许NodeJS:

你可以在NodeJS中使用Phantom(而不是chrome,因为你无法访问任何浏览器 - 也不能在azure web应用程序上运行它们)看到这个例子 - 它在google firebase上托管但你可以轻松应用它到您的NodeJS项目:

https://stackoverflow.com/a/51828577/6306638

如果您需要Chrome,Azure VM上的IIS服务器是您唯一的选择。

如果您需要任何帮助,请告诉我们!

© www.soinside.com 2019 - 2024. All rights reserved.