HiQPdf HTML to PDF Converter allows you to convert the current HTML page to PDF while preserving all the values filled in the HTML form. This can be achieved by rendering a view to an HTML string and converting that HTML string to PDF.
To capture the values filled in the HTML page, you need to render the current view to an HTML string and then convert that HTML string to PDF, passing the page URL as the base URL parameter to the converter. The sample code below demonstrates this technique.
using System;
using System.IO;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.Rendering;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class ConvertCurrentViewController : Controller
{
private readonly ICompositeViewEngine m_viewEngine;
public ConvertCurrentViewController(ICompositeViewEngine viewEngine)
{
m_viewEngine = viewEngine;
}
// GET: ConvertCurrentView
public IActionResult Index()
{
var model = new ConvertCurrentViewViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertToPdf(ConvertCurrentViewViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.SerialNumber = "your-serial-number";
// create a clean ViewDataDictionary containing only the model (simple approach)
ViewDataDictionary viewData = new ViewDataDictionary(ViewData);
viewData.Clear();
viewData.Model = model;
// render the view into an HTML string
StringWriter stringWriter = new StringWriter();
ViewEngineResult viewResult = m_viewEngine.FindView(ControllerContext, "Index", true);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
TempData,
stringWriter,
new HtmlHelperOptions()
);
Task renderTask = viewResult.View.RenderAsync(viewContext);
renderTask.Wait();
string htmlToConvert = stringWriter.ToString();
// build base URL to resolve relative content
HttpRequest httpRequest = ControllerContext.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = httpRequest.Scheme;
uriBuilder.Host = httpRequest.Host.Host;
if (httpRequest.Host.Port != null)
uriBuilder.Port = (int)httpRequest.Host.Port;
uriBuilder.Path = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
uriBuilder.Query = httpRequest.QueryString.ToString();
string thisViewUrl = uriBuilder.Uri.AbsoluteUri;
string baseUrl = thisViewUrl.Substring(0, thisViewUrl.Length - "ConvertCurrentView/ConvertToPdf".Length);
// instantiate the HiQPdf HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// render the HTML code as PDF in memory
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "ConvertThisViewToPdf.pdf";
return fileResult;
}
}
}