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

ExpertPdf Html To Pdf Converter for .NET - Conversion Delay with Html to Pdf Converter - C# / ASP.NET Sample

This sample shows how the html to pdf converter can be used to convert a web page to pdf using ExpertPdf Html To Pdf Library for .NET.

For pages that use javascripts heavily, the conversion can be delayed a number of seconds using converter.ConversionDelay property to allow the content to be fully rendered.

In a similar way, if a page takes too much time to load, the converter can specify the amount of time in seconds when the page load will timeout using the property converter.NavigationTimeout.

Url:

Delay conversion:

seconds

Page timeout:

seconds

Rendering engine:


C# code of this sample

Pages/ConversionDelay.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.HtmlToPdf;
using ExpertPdf.HtmlToPdf.PdfDocument;

namespace ExpertPdf.Samples.Pages
{
    public class ConversionDelayModel : PageModel
    {
        public void OnGet()
        {
        }

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

        [BindProperty]
        public int delay { get; set; }

        [BindProperty]
        public int timeout { get; set; }

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

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

        public IActionResult OnPost()
        {
            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };
            // the rendering engine: Chromium is the recommended engine for modern pages
            converter.RenderingEngine = (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine);

            // specify the number of seconds the conversion is delayed
            converter.ConversionDelay = delay;

            // set the page timeout
            converter.NavigationTimeout = timeout;

            // create a new pdf document converting an url
            Document doc = converter.GetPdfDocumentObjectFromUrl(TxtUrl);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

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