Add Cookies to HTML Page Request

Winnovative HTML to PDF Converter allows you to add HTTP cookies when you request the HTML page. The HTTP cookies to be used when the HTML page to convert is requested can be added to HtmlToPdfConverterHttpRequestCookies collection.

Code Sample - Add Cookies to HTML Page Request

C#
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Winnovative_Next_AspNetDemo.Models;
using Winnovative_Next_AspNetDemo.Models.HTML_to_PDF;

// Use Winnovative Namespace
using Winnovative.Pdf.Next;

namespace Winnovative_Next_AspNetDemo.Controllers.HTML_to_PDF
{
    public class Add_Cookies_to_RequestController : Controller
    {
        public IActionResult Index()
        {
            var model = new Add_Cookies_to_Request_ViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult ConvertHtmlToPdf(Add_Cookies_to_Request_ViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
                throw new ValidationException(errorMessage);
            }

            // Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
            // Licensing.LicenseKey = "your-license-key";

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

            // Add custom HTTP cookies
            // The caller must provide a valid cookie name and RFC 6265-encoded value

            if (!string.IsNullOrEmpty(model.Cookie1Name) && !string.IsNullOrEmpty(model.Cookie1Value))
                htmlToPdfConverter.HttpRequestCookies.Add(model.Cookie1Name, model.Cookie1Value);

            if (!string.IsNullOrEmpty(model.Cookie2Name) && !string.IsNullOrEmpty(model.Cookie2Value))
                htmlToPdfConverter.HttpRequestCookies.Add(model.Cookie2Name, model.Cookie2Value);

            if (!string.IsNullOrEmpty(model.Cookie3Name) && !string.IsNullOrEmpty(model.Cookie3Value))
                htmlToPdfConverter.HttpRequestCookies.Add(model.Cookie3Name, model.Cookie3Value);

            if (!string.IsNullOrEmpty(model.Cookie4Name) && !string.IsNullOrEmpty(model.Cookie4Value))
                htmlToPdfConverter.HttpRequestCookies.Add(model.Cookie4Name, model.Cookie4Value);

            if (!string.IsNullOrEmpty(model.Cookie5Name) && !string.IsNullOrEmpty(model.Cookie5Value))
                htmlToPdfConverter.HttpRequestCookies.Add(model.Cookie5Name, model.Cookie5Value);

            // Convert the HTML page to a PDF document in a memory buffer
            byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(model.Url);

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

            return fileResult;
        }
    }
}

See Also