我正面临着一个恼人的局面。我们尝试在应用程序中使用PuppeteerSharp生成背景PDF,虽然它在开发模式下工作良好,但在生产中却不起作用。
该应用程序是一个WebAPI 2.0站点,.NET4.7.1,Windows 10机器。我认为这两种环境之间的主要区别是:
我们使用以下代码:
var launchOptions = new LaunchOptions
{
DefaultViewport = new ViewPortOptions
{
Width = 1920,
Height = 1080,
IsLandscape = printOptions.Orientation == PrintOrientation.Landscape
},
ExecutablePath = this._chromiumPath,
Timeout = Timeout,
TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory
};
var browser = await Puppeteer.LaunchAsync(launchOptions)
.ConfigureAwait(false);
var page = await browser.NewPageAsync()
.ConfigureAwait(false);
await page.EmulateMediaTypeAsync(PuppeteerSharp.Media.MediaType.Print)
.ConfigureAwait(false);
await page.GoToAsync(url, Timeout, new[] { WaitUntilNavigation.Networkidle0 })
.ConfigureAwait(false);
await page.WaitForTimeoutAsync(2000)
.ConfigureAwait(false);
var options = new PdfOptions
{
Width = printOptions.Format == PrintFormat.A4 ? "210mm" : "297mm",
Height = printOptions.Format == PrintFormat.A4 ? "297mm" : "420mm",
PrintBackground = true,
Landscape = printOptions.Orientation == PrintOrientation.Landscape,
MarginOptions = new PuppeteerSharp.Media.MarginOptions
{
Top = ".4in",
Bottom = ".4in",
Left = ".4in",
Right = ".4in"
}
};
await page.PdfAsync(outputFile, options)
.ConfigureAwait(false);
return result;page.GoToAsync永远不会回来,最终会超时。
编辑:
false。AspNetWebSocketTransport.AspNetTransportFactory传输工厂,它似乎也不起作用发布于 2020-06-17 12:37:17
如果您正在IIS上部署.NET框架应用程序。您还需要使用PuppeteerSharp.AspNetFramwork并将TransportFactory设置为浏览器:
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
Headless = true,
TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
}).ConfigureAwait(false))更新:Nuget包已经过时(硬引用PuppeteerSharp 1.0.0.0),但源代码可以在这里找到:https://github.com/hardkoded/puppeteer-sharp/blob/076897d0cf627c947c61a1192fcb20d968d05cbc/lib/PuppeteerSharp.AspNetFramework/AspNetWebSocketTransport.cs
发布于 2021-08-16 12:10:12
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
Headless = true,
ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
})帮助我解决了这个问题,AspNetWebSocketTransport提供了引用问题,似乎不再有用了。
https://stackoverflow.com/questions/62426256
复制相似问题