Transfer HTML Form Values to Another Page

HiQPdf HTML to PDF Converter allows you to convert another page of the same application. The values you filled in the HTML form will be transferred and used in the other HTML page converted to PDF.

Convert Another HTML Page to PDF

In order to capture the values filled in the HTML page in another HTML page, you first have to transfer the values to other view, render that view to a HTML string and finally convert the HTML string to PDF passing the page URL as base URL parameter to the converter. The sample code below demonstrates this technique.

C#
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.Rendering;

using System.IO;
using HiQPdf.Chromium;

namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
    public class ConvertAnotherViewController : Controller
    {
        private ICompositeViewEngine m_viewEngine;

        public ConvertAnotherViewController(ICompositeViewEngine viewEngine)
        {
            m_viewEngine = viewEngine;
        }

        // GET: ConvertHtmlPreservingState
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ConvertToPdf(IFormCollection formCollection)
        {
            // set a session variable to be used in the the converted view
            ViewDataDictionary viewData = new ViewDataDictionary(ViewData);
            viewData.Clear();

            viewData["MyDataValue"] = formCollection["textBoxDataValue"];

            // get the About view HTML code
            string htmlToConvert = RenderViewAsString("AnotherView", 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 - "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;
        }

        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();
        }
    }
}

See Also