The HiQPdf HTML to PDF and HTML to Image converters can be configured to transmit a set of HTTP cookies to web server when requesting the web page to convert. For example, the HTTP cookies to be transmitted to web server are added to the HttpCookies collection of the HiQPdf.Chromium.HtmlToPdf object using the KeyValueCollectionAdd(String, String) method which takes as parameter the cookie name and the cookie value. Adding custom HTTP cookies to a request can have interesting applications like handling various types of authentication. Below there is a simple sample code for adding a cookie to request.
Set HTTP Cookies Demo
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using HiQPdf.Chromium;
namespace HiQPdf_Chromium_AspNetDemo.Controllers
{
public class CookiesController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
// 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==";
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set HTTP request cookies
if (collection["cookie1NameTextBox"][0].Length > 0 && collection["cookie1ValueTextBox"][0].Length > 0)
htmlToPdfConverter.HttpCookies.Add(collection["cookie1NameTextBox"], collection["cookie1ValueTextBox"]);
if (collection["cookie2NameTextBox"][0].Length > 0 && collection["cookie2ValueTextBox"][0].Length > 0)
htmlToPdfConverter.HttpCookies.Add(collection["cookie2NameTextBox"], collection["cookie2ValueTextBox"]);
if (collection["cookie3NameTextBox"][0].Length > 0 && collection["cookie3ValueTextBox"][0].Length > 0)
htmlToPdfConverter.HttpCookies.Add(collection["cookie3NameTextBox"], collection["cookie3ValueTextBox"]);
if (collection["cookie4NameTextBox"][0].Length > 0 && collection["cookie4ValueTextBox"][0].Length > 0)
htmlToPdfConverter.HttpCookies.Add(collection["cookie4NameTextBox"], collection["cookie4ValueTextBox"]);
if (collection["cookie5NameTextBox"][0].Length > 0 && collection["cookie5ValueTextBox"][0].Length > 0)
htmlToPdfConverter.HttpCookies.Add(collection["cookie5NameTextBox"], collection["cookie5ValueTextBox"]);
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(collection["textBoxUrl"]);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "HTTP_Request_Cookies.pdf";
return fileResult;
}
}
}