I have the following code to save html content as a PDF. I am having consistent issues with blank pages and missing content.
using (Browser browser = await Puppeteer.LaunchAsync(launchOptions))
{
using (var page = await browser.NewPageAsync())
{
await page.SetContentAsync(pageHTML, new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });
// pull in main css from site for formatting
AddTagOptions css = new AddTagOptions() { Url = "https://MYURL.COM/main.css" };
await page.AddStyleTagAsync(css);
// pull in font-awesome css from site for formatting - linking to our own copy doesn't seem to work
AddTagOptions cssFA = new AddTagOptions() { Url = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" };
await page.AddStyleTagAsync(cssFA);
await page.AddStyleTagAsync(new AddTagOptions { Content = inlineCSS });
await page.GetContentAsync();
await page.WaitForSelectorAsync("#lastelement");
var file = await page.PdfDataAsync(pdfOptions);
var fileName = "result.pdf";
return new FileContentResult(file, "application/pdf")
{
FileDownloadName = fileName
};
}
}
Can anyone suggest any tweaks to resolve this problem?
I havce added a network idle check and am waiting for the final element in the supplied HTML (lastelement) but still the same semi-regular blank or incomplete page.