How to use Cookies with IronPDF

Cookies, in the context of web technology, are small pieces of data that websites store on a user's computer or device. They serve various purposes, from session management, where they help keep users logged in, to tracking and analytics, collecting data on user behavior for website improvement. However, the use of cookies has sparked discussions on privacy, leading to regulations like the GDPR and CCPA, and modern web browsers offer users control over cookie management to address these concerns.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet

    PM > Install-Package IronPdf

  2. Copy the code

    new IronPdf.ChromePdfRenderer { RenderingOptions = { RequestContext = IronPdf.Rendering.RequestContexts.Global, CustomCookies = new Dictionary<string, string> { { "sessionId", "your_cookie_value" } } } }
        .RenderUrlAsPdf("https://example.com/protected")
        .SaveAs("secureWithCookies.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Apply Cookies Example

Before using the method to apply cookies, set the RequestContext property to RequestContexts.Global. Then, create the ChromeHttpLoginCredentials class and pass it to the ApplyCookies method. The renderer is now ready to be used for rendering HTML content to PDF with cookies.

Here's an example of applying cookies using IronPDF:

:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-cookies.cs
using IronPdf;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;

ChromeHttpLoginCredentials credentials = new ChromeHttpLoginCredentials() {
    NetworkUsername = "testUser",
    NetworkPassword = "testPassword"
};

string uri = "http://localhost:51169/Invoice";

// Apply cookies
renderer.ApplyCookies(uri, credentials);
Imports IronPdf

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global

Dim credentials As New ChromeHttpLoginCredentials() With {
	.NetworkUsername = "testUser",
	.NetworkPassword = "testPassword"
}

Dim uri As String = "http://localhost:51169/Invoice"

' Apply cookies
renderer.ApplyCookies(uri, credentials)
$vbLabelText   $csharpLabel

RequestContexts Enum: This enum defines browser request contexts used to establish relationships between individual renders. It's essential for managing cookies and user preferences.

  • Isolated: Creates a new request context that is isolated from previous or future renders. Recommended to ensure that the current render is not affected by previous renders.
  • Global: Uses the global request context, which is shared between all renders. Useful in some cases for persisting certain browser states between renders.
  • Auto: Defaults to IronPdf.Rendering.RequestContexts.Isolated but switches to IronPdf.Rendering.RequestContexts.Global if the user has ever invoked IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials).

Apply Custom Cookies Example

Using custom cookies in a request requires setting the CustomCookies property. This property accepts a dictionary of key-value pairs, both as strings.

Here's an example of applying custom cookies using IronPDF:

:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-custom-cookies.cs
using IronPdf;
using System;
using System.Collections.Generic;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

Dictionary<string, string> customCookies = new Dictionary<string, string>();

// Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies;

var uri = new Uri("https://localhost:44362/invoice");
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);
Imports IronPdf
Imports System
Imports System.Collections.Generic

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

Private customCookies As New Dictionary(Of String, String)()

' Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies

Dim uri As New Uri("https://localhost:44362/invoice")
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf(uri)
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I render a cookies-protected webpage to PDF in C#?

You can use IronPDF to render a cookies-protected webpage to PDF by configuring the RequestContext to RequestContexts.Global and using the CustomCookies property to specify your cookies. Then, call the RenderUrlAsPdf method with the URL of the webpage.

What is the role of the RequestContext property in IronPDF?

In IronPDF, the RequestContext property determines how cookies and browser states are managed during PDF rendering. It can be set to Isolated, Global, or Auto to control state persistence between renders.

How can I apply custom cookies when rendering HTML to PDF?

To apply custom cookies in IronPDF, you need to set the CustomCookies property with a dictionary of key-value pairs representing your cookies. These cookies will be applied during the HTML to PDF rendering process.

What methods are used to manage cookies in IronPDF?

IronPDF uses methods like ApplyCookies and properties such as CustomCookies to manage cookies. These tools allow you to incorporate session or custom cookies into your PDF rendering workflow.

Why is it important to manage cookies in PDF rendering?

Managing cookies in PDF rendering is crucial for maintaining user sessions, enabling authentication, and ensuring that the rendered content reflects user-specific data and preferences. It's also important for compliance with privacy regulations like GDPR and CCPA.

How does the ChromeHttpLoginCredentials class assist in cookie management?

The ChromeHttpLoginCredentials class in IronPDF is used to pass login credentials when applying cookies, enabling authentication for sessions during the PDF rendering process.

What are the different types of RequestContexts in IronPDF?

IronPDF offers three types of RequestContexts: Isolated, which creates a new context for each render; Global, which shares contexts across renders; and Auto, which switches based on prior cookie application.

How can I start using IronPDF for cookie management in PDF rendering?

To start using IronPDF for cookie management in PDF rendering, download the library from NuGet, prepare your HTML content, configure the RequestContext property, and utilize the ApplyCookies and CustomCookies methods as needed.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.