Convert a HTML Page to PDF in Same Session

EVO 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.

Code Sample - Convert a HTML Page to PDF in Same Session

C#
using System;
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 System.IO;

// Use EVO PDF Namespace
using EvoPdf.Chromium;

namespace EvoPdf_Chromium_AspNetDemo.Controllers.HTML_to_PDF
{
    public class Convert_Page_in_Same_SessionController : Controller
    {
        private ICompositeViewEngine m_viewEngine;

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

        // GET: Convert_Page_in_Same_Session
        public ActionResult Index()
        {
            ViewData.Add("firstName", "John");
            ViewData.Add("lastName", "Smith");
            ViewData.Add("gender", "maleRadioButton");
            ViewData.Add("haveCar", "true");
            ViewData.Add("carType", "Volvo");
            ViewData.Add("comments", "My comments\r\nLine 1\r\nLine 2");

            return View();
        }

        public ActionResult Display_Session_Variables()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ConvertPageInSameSessionToPdf(IFormCollection collection)
        {
            ViewDataDictionary viewData = new ViewDataDictionary(ViewData);
            viewData.Clear();

            // transmit the posted data to view
            viewData.Add("firstName", collection["firstNameTextBox"]);
            viewData.Add("lastName", collection["lastNameTextBox"]);
            viewData.Add("gender", collection["gender"]);
            viewData.Add("haveCar", collection["haveCarCheckBox"]);
            viewData.Add("carType", collection["carTypeDropDownList"]);
            viewData.Add("comments", collection["commentsTextBox"]);

            // The string writer where to render the HTML code of the view
            StringWriter stringWriter = new StringWriter();

            // Render the Index view in a HTML string
            ViewEngineResult viewResult = m_viewEngine.FindView(ControllerContext, "Display_Session_Variables", false);
            ViewContext viewContext = new ViewContext(
                    ControllerContext,
                    viewResult.View,
                    viewData,
                    TempData,
                    stringWriter,
                    new HtmlHelperOptions()
                    );
            Task renderTask = viewResult.View.RenderAsync(viewContext);
            renderTask.Wait();

            // Get the view HTML string
            string htmlToConvert = stringWriter.ToString();

            // Get the base URL

            HttpRequest request = ControllerContext.HttpContext.Request;
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = request.Scheme;
            uriBuilder.Host = request.Host.Host;
            if (request.Host.Port != null)
                uriBuilder.Port = (int)request.Host.Port;
            uriBuilder.Path = request.PathBase.ToString() + request.Path.ToString();
            uriBuilder.Query = request.QueryString.ToString();

            string currentPageUrl = uriBuilder.Uri.AbsoluteUri;
            string baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Convert_Page_in_Same_Session/ConvertPageInSameSessionToPdf".Length);

            // Create a HTML to PDF converter object with default settings
            HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            // Convert the HTML string to a PDF document in a memory buffer
            byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);

            // Send the PDF file to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "Convert_Page_in_Same_Session.pdf";

            return fileResult;
        }
    }
}

Code Sample - Display Session Variables in Converted HTML Page

XML
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <base href="@Url.Content("~/")" />
    <link href="img/evo.ico" rel="shortcut icon" />
    <link href="styles/styles.css" type="text/css" rel="stylesheet">
    <title>Display Session Variables</title>
</head>
<body>
    <div>
        <b style="font-size: 16px">The session variables</b><br />
        <br />
        <b>First Name:</b>&nbsp;<span id="firstNameLabel">@(ViewData["firstName"] != null ? ViewData["firstName"].ToString() : String.Empty)</span><br />
        <b>Last Name:</b>&nbsp;<span id="lastNameLabel">@(ViewData["lastName"] != null ? ViewData["lastName"].ToString() : String.Empty)</span><br />
        <b>Gender:</b>&nbsp;<span id="genderLabel">@(ViewData["gender"] != null && ViewData["gender"].ToString() == "maleRadioButton" ? "Male" : "Female")</span><br />
        <b>I have a car:</b>&nbsp;<span id="haveCarLabel">@(ViewData["haveCar"] != null && ViewData["haveCar"].ToString() != "false" ? "Yes" : "No")</span><br />
        <div id="carTypePanel" style="display : @(ViewData["haveCar"] != null && ViewData["haveCar"].ToString() == "false" ? "none" : "inline")">
            <b>Car Type:</b>&nbsp;<span id="carTypeLabel">@(ViewData["carType"] != null ? ViewData["carType"].ToString() : String.Empty)</span><br />
        </div>
        <b>Comments:</b>&nbsp;<span id="commentsLabel">@(ViewData["comments"] != null ? ViewData["comments"].ToString() : String.Empty)</span><br />
    </div>
</body>
</html>

See Also