The HiQPdf HTML to PDF and HTML to Image converters can be configured to transmit a set of custom HTTP headers to the web server when requesting the web page to be converted. For example, the HTTP headers to be transmitted to the web server are added to the HttpHeaders collection of the HiQPdf.Next.HtmlToPdf object using the KeyValueCollectionAdd(String, String) method, which takes the HTTP header name and HTTP header value as parameters.
A valid HTTP header name and a printable ASCII value must be provided.
Additionally, you can specify that the HTTP headers should be sent when requesting any resource required by the HTML page, such as images, CSS files or JavaScript files, by setting the HtmlToPdfRepeatHttpHeaders property. By default, this property is set to false and the HTTP headers are sent only when requesting the main HTML content.
Set HTTP Headers Demo
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class HttpHeadersController : Controller
{
public IActionResult Index()
{
var model = new HttpHeadersViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertToPdf(HttpHeadersViewModel 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 an HTML to PDF converter object with default settings
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set the repeat HTTP headers option to control the inclusion of headers
// for each requested resource in the HTML document
htmlToPdfConverter.RepeatHttpHeaders = model.RepeatHttpHeaders;
// Set HTTP request headers
// The caller must provide a valid HTTP header name and printable ASCII value
if (!string.IsNullOrEmpty(model.Header1Name) && !string.IsNullOrEmpty(model.Header1Value))
htmlToPdfConverter.HttpHeaders.Add(model.Header1Name, model.Header1Value);
if (!string.IsNullOrEmpty(model.Header2Name) && !string.IsNullOrEmpty(model.Header2Value))
htmlToPdfConverter.HttpHeaders.Add(model.Header2Name, model.Header2Value);
if (!string.IsNullOrEmpty(model.Header3Name) && !string.IsNullOrEmpty(model.Header3Value))
htmlToPdfConverter.HttpHeaders.Add(model.Header3Name, model.Header3Value);
if (!string.IsNullOrEmpty(model.Header4Name) && !string.IsNullOrEmpty(model.Header4Value))
htmlToPdfConverter.HttpHeaders.Add(model.Header4Name, model.Header4Value);
if (!string.IsNullOrEmpty(model.Header5Name) && !string.IsNullOrEmpty(model.Header5Value))
htmlToPdfConverter.HttpHeaders.Add(model.Header5Name, model.Header5Value);
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(model.Url);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "HTTP_Request_Headers.pdf";
return fileResult;
}
}
}