Search code examples
c#asp.net-mvc-5puppeteer.net-4.7puppeteer-sharp

Puppeteer renders blank pages when not setting breakpoints


I'm using the latest version of PuppeteerSharp and I'm having an issue with blank pages being rendered when not setting a break point.

It would seem the code is not "awaiting" properly if I just let the logic go through without interrupting it.

Here's the code:

using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
}))
{
    using (var page = await browser.NewPageAsync())
    {
        await page.SetContentAsync(htmlDoc.SaveToString()); //this is an HTMLAgilityPack document
        var result = await page.GetContentAsync(); //if I break here for a couple of seconds, it works
        using (var sr = await page.PdfStreamAsync())
        {
            using (var ms = new MemoryStream())
            {
                sr.CopyTo(ms);
                return File(ms.ToArray(), "application/pdf");
            }
        }

     }
 }

Am I doing something wrong? When I get the blank pages, I get the expected number of pages though. Weird.

EDIT: The images are rendered but not the text. Also, I should mention I'm using a Google Front from their website. So it may be that the woff file hasn't downloaded.

Is there a way to wait until page has download all of the remote content before proceeding ?


Solution

  • You could try some wait for navigation options. Something like this:

    await page.SetContentAsync(
        htmlDoc.SaveToString(), 
        new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation. Networkidle0 }});