Sample code · C#
using System; using System.IO; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewEngines; using System.Threading.Tasks; using SelectPdf.Universal; namespace SelectPdf.Samples.Pages { public class CurrentWebPageToPdfModel : PageModel { private readonly IViewRenderService _viewRenderService; public CurrentWebPageToPdfModel(IViewRenderService viewRenderService) { _viewRenderService = viewRenderService; } public void OnGet() { DdlRenderingEngine = "Chromium"; TxtSampleText = "sample text"; DdlDropDownList1 = "Value3"; ChkSampleCheckbox = true; TxtSampleText2 = "1000"; TxtSampleText3 = "800"; } [BindProperty] public string TxtSampleText { 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 DdlDropDownList1 { get; set; } public List<SelectListItem> DropDownList1Values { get; } = new List<SelectListItem> { new SelectListItem { Value = "Value1", Text = "Value1" }, new SelectListItem { Value = "Value2", Text = "Value2" }, new SelectListItem { Value = "Value3", Text = "Value3" }, new SelectListItem { Value = "Value4", Text = "Value4" }, }; [BindProperty] public bool ChkSampleCheckbox { get; set; } [BindProperty] public string TxtSampleText2 { get; set; } [BindProperty] public string TxtSampleText3 { get; set; } public async Task<IActionResult> OnPost() { RenderingEngine renderingEngine = SampleForm.ParseEnum(DdlRenderingEngine, RenderingEngine.Chromium); // Render this page to a full HTML string using the CONVERTER layout // (flattened sticky nav / overflow rail) so the produced PDF // paginates cleanly, mirroring the live inner-page look. string htmlString = await _viewRenderService.RenderToStringAsync("CurrentWebPageToPdf", "~/Pages/_LayoutConverter.cshtml", new CurrentWebPageToPdfModel(_viewRenderService) { TxtSampleText = this.TxtSampleText, DdlDropDownList1 = this.DdlDropDownList1, ChkSampleCheckbox = this.ChkSampleCheckbox, TxtSampleText2 = this.TxtSampleText2, TxtSampleText3 = this.TxtSampleText3, }); // Base URL the engine resolves the captured page's RELATIVE assets // (assets/css/spdf.css, fonts, icons) against. Must be the app-root // HTTP origin INCLUDING the PathBase and a trailing slash — NOT a // filesystem path (Startup.WebRootPath), which loads no stylesheets // and yields an unstyled PDF. string baseUrl = Helper.AppBaseUrl(Request); // instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); converter.Options.NavigationTimeout = Startup.NavigationTimeout; converter.Options.RenderingEngine = renderingEngine; // create a new pdf document converting a html string PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl); // 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; } } }