IIS 中的 PuppeteerSharp - ASP.NET Web 窗体

PuppeteerSharp in IIS - ASP.NET Web Forms

尝试在我们的 ASP.NET Web Forms 应用程序中使用 PuppeteerSharp 生成 PDF,虽然它在本地工作正常,但在部署到 IIS 服务器时收到以下错误。

我注意到的一件事是创建了一个名为 download-Win64-706915.zip 的零字节 ZIP 文件。

我的代码如下

var url = ConfigurationManager.AppSettings["EnrolmentFormUrl"] + "?code=" + code;

var fileName = "enrolment" + $"{DateTime.Now:__ddMMyyyyhhmmssfff}" + ".pdf";

var output = Config.attachmentPath + fileName;                     

var browserFetcher = new BrowserFetcher(new BrowserFetcherOptions
{
    Path = Config.attachmentPath
});;

await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).ConfigureAwait(false);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true,
    TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
    ExecutablePath = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath
}).ConfigureAwait(false);

var pdfOptions = new PdfOptions
{
    Format = PaperFormat.A4,
    PreferCSSPageSize = true
};

var page = await browser.NewPageAsync();
await page.GoToAsync(url).ConfigureAwait(false);
await page.PdfAsync(output, pdfOptions).ConfigureAwait(false);

问题是由于部署到的服务器没有互联网访问权限,PuppeteerSharp 正在寻找下载 Chromium,通过指定本地执行路径解决。

ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

修改后的代码

var url = ConfigurationManager.AppSettings["EnrolmentFormUrl"] + "?code=" + code;

var fileName = "enrolment" + $"{DateTime.Now:__ddMMyyyyhhmmssfff}" + ".pdf";

var output = Config.attachmentPath + fileName;                     

var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true,
    TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
    ExecutablePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
}).ConfigureAwait(false);

var pdfOptions = new PdfOptions
{
    Format = PaperFormat.A4,
    PreferCSSPageSize = true
};

var page = await browser.NewPageAsync();
await page.GoToAsync(url).ConfigureAwait(false);
await page.PdfAsync(output, pdfOptions).ConfigureAwait(false);