如何在Azure云服务或Azure Functions中运行Headless Chrome? [英] How to run Headless Chrome in Azure Cloud Service or Azure Functions?
问题描述
我正在尝试使用Headless Chrome从复杂的HTML文件(包含图像,SVG等)生成PDF文件.我可以在Cloud Service(Windows)上使用wkhtmltopdf.exe生成简单的PDF文件,但是我确实需要Chrome生成尽可能接近HTML + SVG +图像的PDF.
I am trying to use Headless Chrome to generate a PDF file from a complex HTML file (contains images, SVGs, etc.). I am able to use wkhtmltopdf.exe on Cloud Service (Windows) to generate simple PDF file, but I really need Chrome to produce PDFs as close as possible to the HTML + SVG + Image.
我希望能够在Azure Cloud Service或Azure Functions中运行Headless Chrome,但是我无法使其正常运行.我想这是由于对GDI的限制.我可以在自己的计算机上的Azure仿真器中运行我的代码和无头Chrome,但是一旦部署,将无法正常工作.
I was hoping to be able to run Headless Chrome in Azure Cloud Service or Azure Functions, but I cannot get it to work. I suppose this is due to restrictions on GDI. I was able to run my code and Headless Chrome in the Azure Emulator on my own machine, but once it is deployed nothing works.
以下是我当前在Azure函数(对于Windows)中运行的代码.我正在使用Puppeteer拍摄example.com的屏幕截图.如果我能使它正常工作,我想生成PDF将会变得很容易.
Below is the code I am currently running in Azure Functions (for Windows). I am using Puppeteer to take a screenshot of example.com. If I can get this to work, I suppose that generating PDF will become easy.
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);
};
下面是尝试执行脚本时的日志.
Below is the log when trying to execute the script.
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确保我使用的路径正确.
The error "Failure = 'Error: spawn UNKNOWN'" is not clear. I made sure that the path I am using is correct using Kudu and PowerShell.
我正在寻找一种在Azure Cloud Service和/或Azure Functions上运行Chrome的方法(对于Windows-为了使用我现有的App Service计划).是否有人尝试在Azure中运行Headless Chrome?我愿意接受任何可以帮助我使此脚本正常工作的想法吗?
I am looking for a way to run Chrome on Azure Cloud Service and/or Azure Functions (for Windows - in order to use my existing App Service plan). Anybody has also attempted to run Headless Chrome in Azure? I am open to any ideas which would help me to get this script to work?
推荐答案
我建议使用 https://www.browserless.io/,因此您无需在应用程序服务中运行chrome.exe.
I would recommend to use https://www.browserless.io/ so you don't have to run the chrome.exe in the app service.
用puppeteer.connect替换puppeteer.launch
Replace puppeteer.launch with puppeteer.connect
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io/'
});
这篇关于如何在Azure云服务或Azure Functions中运行Headless Chrome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!