HTTP Request Header

This code demonstrates how to use HTTP request headers in IronPDF to render a PDF from an authenticated webpage. It begins by creating an instance of ChromePdfRenderer, which is responsible for converting web pages or HTML into PDFs. Next, it sets up HTTP request headers by assigning a dictionary to the HttpRequestHeaders property within RenderingOptions. In this case, an Authorization header is added with a Bearer token (test-token-123), which is commonly used for authentication in APIs.

Once the headers are set, the RenderUrlAsPdf method is used to fetch and convert the webpage at https://httpbin.org/bearer into a PDF. This particular URL is a test API that requires Bearer authentication, making it a useful way to verify that the header is being correctly sent. Finally, the resulting PDF file is saved as "output.pdf" in the current directory. This approach is beneficial for generating PDFs from authenticated web pages, such as protected reports, dashboards, or API-generated content.

// Import the necessary namespace for IronPDF
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Set up the rendering options to include authorization headers
        var renderingOptions = new RenderingOptions();

        // Assign a dictionary of HTTP request headers
        renderingOptions.HttpRequestHeaders = new System.Collections.Generic.Dictionary<string, string>
        {
            // Add Authorization header with Bearer token
            { "Authorization", "Bearer test-token-123" }
        };

        // Use the renderer with the specified options to render the URL into a PDF
        var pdf = renderer.RenderUrlAsPdf("https://httpbin.org/bearer", renderingOptions);

        // Save the rendered PDF file in the current directory as "output.pdf"
        pdf.SaveAs("output.pdf");
    }
}
// Import the necessary namespace for IronPDF
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Set up the rendering options to include authorization headers
        var renderingOptions = new RenderingOptions();

        // Assign a dictionary of HTTP request headers
        renderingOptions.HttpRequestHeaders = new System.Collections.Generic.Dictionary<string, string>
        {
            // Add Authorization header with Bearer token
            { "Authorization", "Bearer test-token-123" }
        };

        // Use the renderer with the specified options to render the URL into a PDF
        var pdf = renderer.RenderUrlAsPdf("https://httpbin.org/bearer", renderingOptions);

        // Save the rendered PDF file in the current directory as "output.pdf"
        pdf.SaveAs("output.pdf");
    }
}
' Import the necessary namespace for IronPDF
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an instance of ChromePdfRenderer
		Dim renderer = New ChromePdfRenderer()

		' Set up the rendering options to include authorization headers
		Dim renderingOptions As New RenderingOptions()

		' Assign a dictionary of HTTP request headers
		renderingOptions.HttpRequestHeaders = New System.Collections.Generic.Dictionary(Of String, String) From {
			{"Authorization", "Bearer test-token-123"}
		}

		' Use the renderer with the specified options to render the URL into a PDF
		Dim pdf = renderer.RenderUrlAsPdf("https://httpbin.org/bearer", renderingOptions)

		' Save the rendered PDF file in the current directory as "output.pdf"
		pdf.SaveAs("output.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel