ExpertPdf Anywhere ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Anywhere - 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 waits for the page to say it is ready; ConversionDelay is a fixed wait.

Url:


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

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.Anywhere.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";

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


            // 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;
        }
    }
}