I am facing a bit of an annoying situation. We try to use PuppeteerSharp in our application to generate background PDF, and while it works well in dev mode, it doesn't work when in production.
The app is a WebAPI 2.0 site, .NET4.7.1, Windows 10 machine. The main differences I would see beween the two environments are:
- build in Release instead of Debug: calling my code from a console app either in Debug or Release mode seems to work in the same way
- Hosting in IIS Express in development and full IIS in Production
We use the following code:
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
never returns, and eventually times out.
Edit:
- I set ConfigureAwait to
false
in all async calls - I tried using the
AspNetWebSocketTransport.AspNetTransportFactory
transport factory, which doesn't seem to work either