SelectPdf for .NET 10 - Convert from Html to Pdf - C# / .NET 10 Sample

This sample shows how to use SelectPdf html to pdf converter to convert an url to pdf, also setting a few properties.

SelectPdf.Universal uses the Chromium rendering engine (HTML5/CSS3/JS), available on all .NET platforms and fully supported on Azure Web Apps.

Url:

Rendering Engine:


Pdf Page Size:


Pdf Page Orientation:


Web Page Width:
px

Web Page Height:
px
(leave empty to auto detect,
or set it to leave out the content below)

Fit wide content
(lay out content wider than the web page
width at its own width and scale it down,
instead of cutting it at the right edge)

WebGL enabled
(turn off for faster conversions
of pages that do not use WebGL)

Sample code · C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class ConvertUrlToPdfModel : PageModel
    {
        public void OnGet()
        {
            DdlPageSize = "A4";
        }

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

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

        public List<SelectListItem> RenderingEngines { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Chromium", Text = "Chromium" },
        };

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

        public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "A1", Text = "A1" },
            new SelectListItem { Value = "A2", Text = "A2" },
            new SelectListItem { Value = "A3", Text = "A3" },
            new SelectListItem { Value = "A4", Text = "A4" },
            new SelectListItem { Value = "A5", Text = "A5" },
            new SelectListItem { Value = "Letter", Text = "Letter" },
            new SelectListItem { Value = "HalfLetter", Text = "HalfLetter" },
            new SelectListItem { Value = "Ledger", Text = "Ledger" },
            new SelectListItem { Value = "Legal", Text = "Legal" },
        };

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

        public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Portrait", Text = "Portrait" },
            new SelectListItem { Value = "Landscape", Text = "Landscape" },
        };

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

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

        [BindProperty]
        public bool ChkFitWideContent { get; set; } = false;   // the library default: wide content is cut, as a browser cuts it

        [BindProperty]
        public bool ChkWebGl { get; set; } = false;   // the library default; on only for pages that draw with WebGL

        public IActionResult OnPost()
        {
            // read parameters from the webpage
            RenderingEngine renderingEngine =
                SampleForm.ParseEnum(DdlRenderingEngine, RenderingEngine.Chromium);

            PdfPageSize pageSize =
                SampleForm.ParseEnum(DdlPageSize, PdfPageSize.A4);

            PdfPageOrientation pdfOrientation =
                SampleForm.ParseEnum(DdlPageOrientation, PdfPageOrientation.Portrait);

            int webPageWidth = 1024;
            try
            {
                webPageWidth = System.Convert.ToInt32(TxtWidth);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = System.Convert.ToInt32(TxtHeight);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.NavigationTimeout = Startup.NavigationTimeout;

            // set converter options
            converter.Options.RenderingEngine = renderingEngine;
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;
            // a height alone only sizes the viewport; fixing the size cuts the content to it
            converter.Options.WebPageFixedSize = webPageHeight > 0;
            // keep content wider than the web page width (a wide table, a long line) by laying the page
            // out at its own width and scaling it down to the pdf page; off, it is cut at the right edge
            converter.Options.FitWideContent = ChkFitWideContent;
            // WebGL off skips the GPU bring-up, which makes a conversion faster
            converter.Options.WebGlEnabled = ChkWebGl;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(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 = "Document.pdf";
            return fileResult;

        }
    }
}