HTML to PDF Converter ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Html To Pdf Converter for .NET - Wait until the Page is Ready - C# / ASP.NET Sample

Pages that load their content with scripts are not finished when the browser says they are loaded. A page can tell the converter when it is ready by setting window.status: with WindowStatus = "ready" the converter waits until the page sets window.status = "ready" and then converts it. If the page never does, the conversion ends with a navigation timeout.

The test page builds its report two seconds after it loads. Convert it with and without waiting to see the difference. WindowStatus is honored by the Chromium and Blink engines; ConversionDelay is a fixed wait that works with every engine.

Url:


Wait for window.status to be:
(leave empty to convert the page as soon as it is loaded)

Rendering engine:


C# code of this sample

Pages/WindowStatus.cshtml.cs
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.HtmlToPdf;

namespace ExpertPdf.Samples.Pages
{
    public class WindowStatusModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/window-status.html");
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        [BindProperty]
        public string TxtWindowStatus { get; set; } = "ready";

        [BindProperty]
        public string DdlRenderingEngine { get; set; } = "Chromium";

        public List<SelectListItem> RenderingEngines { get; } = Helper.BrowserRenderingEngines();

        public IActionResult OnPost()
        {
            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };
            // converter.LicenseKey = "your license key";

            // the rendering engine: WindowStatus is honored by Chromium (recommended) and Blink
            converter.RenderingEngine = (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine);

            // wait until the page sets window.status to this text; give up after 30 seconds
            if (!string.IsNullOrEmpty(TxtWindowStatus))
                converter.WindowStatus = TxtWindowStatus;
            converter.NavigationTimeout = 30;

            // convert the url to pdf
            byte[] pdf = converter.GetPdfBytesFromUrl(TxtUrl);

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "WindowStatus.pdf";
            return fileResult;
        }
    }
}