Add HTTP Headers to HTML Page Request

Winnovative HTML to PDF Converter allows you to add HTTP headers when you request the HTML page. There are various standard HTTP headers offering important information to web server about the capabilities of the browser like the accepted content type, accepted encoding, accepted language, connection mode, user agent.

The HTTP headers to be used when the HTML page to convert is requested can be added to HtmlToPdfConverterHttpRequestHeaders collection.

Additionally, it is possible to specify that the HTTP headers should be sent when requesting any resource required by the HTML page, such as images, CSS files, JavaScript files, by setting the HtmlToPdfConverterPersistentHttpRequestHeaders property. By default, this property is set to false and the HTTP headers are sent only when requesting the main HTML content.

Code Sample - Add HTTP Headers to HTML Page Request

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

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

namespace Winnovative_Chromium_AspNetDemo.Controllers.HTML_to_PDF
{
    public class Add_HTTP_Headers_to_RequestController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ConvertHtmlToPdf(IFormCollection collection)
        {
            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the library in demo mode
            Licensing.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

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

            // Set the persistent HTTP Headers option to control the inclusion of headers 
            // for each requested resource in the HTML document
            htmlToPdfConverter.PersistentHttpRequestHeaders = collection["persistentHttpHeadersCheckBox"].Count > 0;

            // Add custom HTTP headers

            if (collection["header1NameTextBox"][0].Length > 0 && collection["header1ValueTextBox"][0].Length > 0)
                htmlToPdfConverter.HttpRequestHeaders.Add(collection["header1NameTextBox"], collection["header1ValueTextBox"]);

            if (collection["header2NameTextBox"][0].Length > 0 && collection["header2ValueTextBox"][0].Length > 0)
                htmlToPdfConverter.HttpRequestHeaders.Add(collection["header2NameTextBox"], collection["header2ValueTextBox"]);

            if (collection["header3NameTextBox"][0].Length > 0 && collection["header3ValueTextBox"][0].Length > 0)
                htmlToPdfConverter.HttpRequestHeaders.Add(collection["header3NameTextBox"], collection["header3ValueTextBox"]);

            if (collection["header4NameTextBox"][0].Length > 0 && collection["header4ValueTextBox"][0].Length > 0)
                htmlToPdfConverter.HttpRequestHeaders.Add(collection["header4NameTextBox"], collection["header4ValueTextBox"]);

            if (collection["header5NameTextBox"][0].Length > 0 && collection["header5ValueTextBox"][0].Length > 0)
                htmlToPdfConverter.HttpRequestHeaders.Add(collection["header5NameTextBox"], collection["header5ValueTextBox"]);

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

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

            return fileResult;
        }
    }
}

See Also