HiQPdf HTML to PDF Converter allows you to convert the current HTML page to PDF preserving all the values filled in HTML form. This can be achieved by rendering a view to a HTML string and converting that HTML string to PDF.
In order to capture the values filled in the HTML page, you have to render the current view to a HTML string and then convert that HTML string to PDF passing the page URL as base URL parameter to the converter. The sample code below demonstrates this technique.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System.IO;
using System;
using HiQPdf.Chromium;
namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
public class ConvertCurrentViewController : Controller
{
private ICompositeViewEngine m_viewEngine;
public ConvertCurrentViewController(ICompositeViewEngine viewEngine)
{
m_viewEngine = viewEngine;
}
// GET: ConvertCurrentView
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection formCollection)
{
// Replace the demo serial number with the serial number received upon purchase
// to run the converter in licensed mode
Licensing.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// add the custom value to view data
ViewDataDictionary viewData = new ViewDataDictionary(ViewData);
viewData.Clear();
viewData.Add("MyTextValue", formCollection["textBoxText"]);
viewData.Add("MyDropDownListValue", formCollection["dropDownListValues"]);
// get the HTML code of this view
string htmlToConvert = RenderViewAsString("Index", viewData);
// the base URL to resolve relative images and css
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;
}
public string RenderViewAsString(string viewName, ViewDataDictionary viewData)
{
// create a string writer to receive the HTML code
StringWriter stringWriter = new StringWriter();
// get the view to render
ViewEngineResult viewResult = m_viewEngine.FindView(ControllerContext, viewName, true);
// create a context to render a view based on a model
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
TempData,
stringWriter,
new HtmlHelperOptions()
);
// render the view to a HTML code
viewResult.View.RenderAsync(viewContext).Wait();
// return the HTML code
return stringWriter.ToString();
}
}
}