HiQPdf HTML to PDF Converter allows you to convert another page of the same application. The values you filled in the HTML form are transferred and used in the other HTML page that is converted to PDF.
To capture the values filled in the HTML page in another HTML page, you first need to transfer the values to another view, render that view to an HTML string and finally convert the 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.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.Rendering;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class ConvertAnotherViewController : Controller
{
private readonly ICompositeViewEngine m_viewEngine;
public ConvertAnotherViewController(ICompositeViewEngine viewEngine)
{
m_viewEngine = viewEngine;
}
// GET: ConvertAnotherView
public IActionResult Index()
{
var model = new ConvertAnotherViewViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertToPdf(ConvertAnotherViewViewModel 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
ViewDataDictionary viewData = new ViewDataDictionary(ViewData);
viewData.Clear();
viewData.Model = model;
// render the AnotherView view into an HTML string
StringWriter stringWriter = new StringWriter();
ViewEngineResult viewResult = m_viewEngine.FindView(ControllerContext, "AnotherView", 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 - "ConvertAnotherView/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);
// send the PDF document to browser
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "ConvertAnotherViewToPdf.pdf";
return fileResult;
}
}
}