Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, we explore the process of using cookies with Iron PDF for secure PDF generation in C# applications. The video guides viewers through setting up an ASP.NET Core application with a protected invoice page, which requires network login authentication. Viewers learn how to utilize Iron PDF to convert this page into a PDF document, using cookies to maintain session-specific information.
Starting with the installation of the Iron PDF NuGet package, the tutorial walks through importing necessary namespaces and setting up the Chrome renderer class. By configuring the global request context property to include cookies, the tutorial ensures the preservation of session data during rendering. Detailed instructions on defining login credentials, applying cookies, and rendering the page as a PDF are provided.
The video also demonstrates how to manually set custom cookies and save the final PDF document to a specified path. By following these steps, developers can efficiently convert HTML pages to PDFs while securely managing authentication and session data. The tutorial concludes by encouraging viewers to sign up for a trial of Iron PDF to explore its capabilities firsthand.
Further Reading: How to use HTTP Request Header
Here is a sample C# code implementation illustrating the concepts discussed:
using System;
using IronPdf;
using System.Collections.Generic;
namespace PdfGenerationExample
{
class Program
{
static void Main(string[] args)
{
// Initialize IronPDF renderer
var Renderer = new ChromePdfRenderer();
// Set up the request headers using cookies to maintain session information
var globalRequestHeaders = new Dictionary<string, string>
{
{ "Cookie", "session_id=your_session_id; auth_token=your_auth_token;" }
};
// Configure the Renderer to use the global request headers
Renderer.RenderingOptions.HttpRequestHeaders = globalRequestHeaders;
// The URL of the protected invoice page
string url = "https://example.com/protected-invoice";
try
{
// Render the HTML page to PDF
var pdfDocument = Renderer.RenderUrlAsPdf(url);
// Save the PDF to a specified path
pdfDocument.SaveAs("InvoiceDocument.pdf");
Console.WriteLine("PDF generation successful!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
using System;
using IronPdf;
using System.Collections.Generic;
namespace PdfGenerationExample
{
class Program
{
static void Main(string[] args)
{
// Initialize IronPDF renderer
var Renderer = new ChromePdfRenderer();
// Set up the request headers using cookies to maintain session information
var globalRequestHeaders = new Dictionary<string, string>
{
{ "Cookie", "session_id=your_session_id; auth_token=your_auth_token;" }
};
// Configure the Renderer to use the global request headers
Renderer.RenderingOptions.HttpRequestHeaders = globalRequestHeaders;
// The URL of the protected invoice page
string url = "https://example.com/protected-invoice";
try
{
// Render the HTML page to PDF
var pdfDocument = Renderer.RenderUrlAsPdf(url);
// Save the PDF to a specified path
pdfDocument.SaveAs("InvoiceDocument.pdf");
Console.WriteLine("PDF generation successful!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
Imports System
Imports IronPdf
Imports System.Collections.Generic
Namespace PdfGenerationExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Initialize IronPDF renderer
Dim Renderer = New ChromePdfRenderer()
' Set up the request headers using cookies to maintain session information
Dim globalRequestHeaders = New Dictionary(Of String, String) From {
{"Cookie", "session_id=your_session_id; auth_token=your_auth_token;"}
}
' Configure the Renderer to use the global request headers
Renderer.RenderingOptions.HttpRequestHeaders = globalRequestHeaders
' The URL of the protected invoice page
Dim url As String = "https://example.com/protected-invoice"
Try
' Render the HTML page to PDF
Dim pdfDocument = Renderer.RenderUrlAsPdf(url)
' Save the PDF to a specified path
pdfDocument.SaveAs("InvoiceDocument.pdf")
Console.WriteLine("PDF generation successful!")
Catch ex As Exception
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
End Sub
End Class
End Namespace
Imports and Namespace: Start by importing necessary namespaces. We use IronPdf
for PDF rendering, and System.Collections.Generic
for working with collections, such as dictionaries.
Initialize ChromePdfRenderer: ChromePdfRenderer
is initialized to handle the conversion of URLs to PDF documents.
Set up Request Headers: We prepare a dictionary globalRequestHeaders
that holds cookie information required for the session. This is crucial for accessing protected resources which require authentication.
Configuration: The renderer is configured to utilize these headers through RenderingOptions.HttpRequestHeaders
.
By following this example, you can learn how to use Iron PDF for secure PDF generation, maintaining authentication throughout.