Convert HTML to PDF Behind Login Authentication in C#
To convert HTML to PDF behind login authentication in C#, use IronPDF's ChromeHttpLoginCredentials for network authentication or download HTML using System.Net.WebClient/HttpClient before conversion. This approach handles both network authentication and HTML form logins effectively.
Quickstart: Convert HTML to PDF Behind Login with IronPDF
Convert HTML pages to PDFs behind login forms using IronPDF's API. This guide demonstrates ChromeHttpLoginCredentials for authentication and protected content retrieval. Handle both network authentication and HTML form logins with straightforward code examples.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
new ChromePdfRenderer { LoginCredentials = new ChromeHttpLoginCredentials("username","password") } .RenderUrlAsPdf("https://example.com/protected") .SaveAs("secure.pdf");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# IronPDF Library
- Download the HTML to Avoid Logins
- Login using Network Authentication with
LoginCredentialsproperty - Use HTML Form for Authentication
- Workaround for MVC Login Authentication
What Are the Best Practices for Handling Login Authentication?
IronPDF supports TLS network authentication (username and password) through the ChromeHttpLoginCredentials API. For comprehensive guidance on various login scenarios, see the TLS Website & System Logins tutorial.
The recommended approach is using System.Net.WebClient or HttpClient to download HTML and assets. This method supports headers, logins, and other requirements. After downloading to memory or disk, IronPDF converts the HTML to PDF. Extract assets like stylesheets and images using HtmlAgilityPack, then download with System.Net.WebClient.
// Download HTML content from a URL with authentication
string html;
using (WebClient client = new WebClient())
{
// Add authentication headers if needed
client.Headers.Add("Authorization", "Bearer " + accessToken);
// Download the HTML string
html = client.DownloadString("http://www.example.com/protected-content");
}
// Load the HTML into an HtmlDocument for parsing
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
// Extract all image sources for downloading
foreach(HtmlNode img in doc.DocumentNode.SelectNodes("//img"))
{
string imgSrc = img.GetAttributeValue("src", null);
Console.WriteLine($"Found image: {imgSrc}");
// Download each image asset
if (!string.IsNullOrEmpty(imgSrc))
{
string fileName = Path.GetFileName(imgSrc);
client.DownloadFile(imgSrc, fileName);
}
}
// Convert the downloaded HTML to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("authenticated-content.pdf");// Download HTML content from a URL with authentication
string html;
using (WebClient client = new WebClient())
{
// Add authentication headers if needed
client.Headers.Add("Authorization", "Bearer " + accessToken);
// Download the HTML string
html = client.DownloadString("http://www.example.com/protected-content");
}
// Load the HTML into an HtmlDocument for parsing
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
// Extract all image sources for downloading
foreach(HtmlNode img in doc.DocumentNode.SelectNodes("//img"))
{
string imgSrc = img.GetAttributeValue("src", null);
Console.WriteLine($"Found image: {imgSrc}");
// Download each image asset
if (!string.IsNullOrEmpty(imgSrc))
{
string fileName = Path.GetFileName(imgSrc);
client.DownloadFile(imgSrc, fileName);
}
}
// Convert the downloaded HTML to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("authenticated-content.pdf");System.Uri constructor. To rebase all relative paths in an HTML document, add a <base> tag to the header using HtmlAgilityPack. Example. For more information on handling URLs and assets, see the Base URLs & Asset Encoding guide.Why Should I Download HTML Content First?
Downloading HTML content before conversion provides several advantages:
- Complete Control: Modify HTML, fix broken links, or inject authentication tokens before conversion
- Asset Management: Download and cache external resources like images, CSS, and JavaScript files
- Authentication Flexibility: Use any .NET authentication mechanism including OAuth, JWT tokens, or custom headers
- Performance: Cache frequently accessed content to reduce server load
- Debugging: Inspect the exact HTML being converted to troubleshoot issues
For complex authentication scenarios involving cookies and sessions, see the Cookies guide which explains authentication state management during PDF conversion.
How Do I Handle Assets Like Images and Stylesheets?
When converting authenticated pages, external assets often require the same authentication. Here's a comprehensive approach using HttpClient:
public async Task<string> DownloadAuthenticatedHtmlWithAssets(string url, string authToken)
{
using (var client = new HttpClient())
{
// Set authentication header
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authToken);
// Download the main HTML
string html = await client.GetStringAsync(url);
// Parse HTML to find assets
var doc = new HtmlDocument();
doc.LoadHtml(html);
// Create a base URI for resolving relative paths
var baseUri = new Uri(url);
// Download CSS files
var cssLinks = doc.DocumentNode.SelectNodes("//link[@rel='stylesheet']");
if (cssLinks != null)
{
foreach (var link in cssLinks)
{
string href = link.GetAttributeValue("href", "");
if (!string.IsNullOrEmpty(href))
{
var cssUri = new Uri(baseUri, href);
string cssContent = await client.GetStringAsync(cssUri);
// Embed CSS directly in the HTML
var styleNode = doc.CreateElement("style");
styleNode.InnerHtml = cssContent;
doc.DocumentNode.SelectSingleNode("//head").AppendChild(styleNode);
// Remove the external link
link.Remove();
}
}
}
// Return the modified HTML with embedded assets
return doc.DocumentNode.OuterHtml;
}
}public async Task<string> DownloadAuthenticatedHtmlWithAssets(string url, string authToken)
{
using (var client = new HttpClient())
{
// Set authentication header
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authToken);
// Download the main HTML
string html = await client.GetStringAsync(url);
// Parse HTML to find assets
var doc = new HtmlDocument();
doc.LoadHtml(html);
// Create a base URI for resolving relative paths
var baseUri = new Uri(url);
// Download CSS files
var cssLinks = doc.DocumentNode.SelectNodes("//link[@rel='stylesheet']");
if (cssLinks != null)
{
foreach (var link in cssLinks)
{
string href = link.GetAttributeValue("href", "");
if (!string.IsNullOrEmpty(href))
{
var cssUri = new Uri(baseUri, href);
string cssContent = await client.GetStringAsync(cssUri);
// Embed CSS directly in the HTML
var styleNode = doc.CreateElement("style");
styleNode.InnerHtml = cssContent;
doc.DocumentNode.SelectSingleNode("//head").AppendChild(styleNode);
// Remove the external link
link.Remove();
}
}
}
// Return the modified HTML with embedded assets
return doc.DocumentNode.OuterHtml;
}
}What Tools Help with HTML Parsing?
The HtmlAgilityPack is the most popular HTML parsing library for .NET, but alternatives exist:
- HtmlAgilityPack: Best for general HTML parsing and manipulation
- AngleSharp: Modern, standards-compliant HTML parser with CSS selector support
- CsQuery: jQuery-like syntax for C# developers familiar with jQuery
- Regular Expressions: For simple extraction tasks (not recommended for complex HTML)
How Do I Login Using Network Authentication?
Most ASP.NET applications support network authentication, which is more reliable than HTML form posting. IronPDF provides built-in support for basic, digest, and NTLM authentication through the ChromeHttpLoginCredentials class. For additional header customization, see the HTTP Request Header guide.
:path=/static-assets/pdf/content-code-examples/how-to/logins-username-password.csusing IronPdf;
using System;
ChromePdfRenderer renderer = new ChromePdfRenderer
{
// setting login credentials to bypass basic authentication
LoginCredentials = new ChromeHttpLoginCredentials()
{
NetworkUsername = "testUser",
NetworkPassword = "testPassword"
}
};
var uri = new Uri("http://localhost:51169/Invoice");
// Render web URL to PDF
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);
// Export PDF
pdf.SaveAs("UrlToPdfExample.Pdf");Why Is Network Authentication More Reliable Than Form Posting?
Network authentication offers several advantages over HTML form posting:
- Standardized Protocol: Uses HTTP authentication headers following RFC standards
- Browser Integration: Chrome rendering engine handles authentication seamlessly
- Session Management: Automatic handling of authentication challenges and session persistence
- Security: Credentials transmitted securely via headers rather than form data
- Compatibility: Works with most enterprise authentication systems (
Active Directory,LDAP)
What Credentials Do I Need for Network Authentication?
Different authentication types require different credentials:
// Basic Authentication (most common)
var basicAuth = new ChromeHttpLoginCredentials
{
NetworkUsername = "user@domain.com",
NetworkPassword = "password123",
AuthenticationType = ChromeHttpLoginCredentials.AuthType.Basic
};
// NTLM/Windows Authentication
var ntlmAuth = new ChromeHttpLoginCredentials
{
NetworkUsername = "DOMAIN\\username", // Include domain
NetworkPassword = "password123",
AuthenticationType = ChromeHttpLoginCredentials.AuthType.Ntlm
};
// Custom authentication headers
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CustomHttpHeaders.Add("X-API-Key", "your-api-key");
renderer.RenderingOptions.CustomHttpHeaders.Add("Authorization", "Bearer " + jwtToken);// Basic Authentication (most common)
var basicAuth = new ChromeHttpLoginCredentials
{
NetworkUsername = "user@domain.com",
NetworkPassword = "password123",
AuthenticationType = ChromeHttpLoginCredentials.AuthType.Basic
};
// NTLM/Windows Authentication
var ntlmAuth = new ChromeHttpLoginCredentials
{
NetworkUsername = "DOMAIN\\username", // Include domain
NetworkPassword = "password123",
AuthenticationType = ChromeHttpLoginCredentials.AuthType.Ntlm
};
// Custom authentication headers
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CustomHttpHeaders.Add("X-API-Key", "your-api-key");
renderer.RenderingOptions.CustomHttpHeaders.Add("Authorization", "Bearer " + jwtToken);How Do I Troubleshoot Authentication Failures?
Common authentication issues and solutions:
- 401 Unauthorized: Check credentials and authentication type
- 403 Forbidden: User authenticated but lacks permissions
- Timeout Errors: Increase
RenderDelayfor slow authentication systems - Certificate Errors: Configure TLS/SSL settings appropriately
Enable debugging to diagnose issues:
// Enable detailed logging
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "IronPdf.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Test authentication
try
{
var pdf = renderer.RenderUrlAsPdf("https://secure.example.com");
pdf.SaveAs("authenticated.pdf");
}
catch (Exception ex)
{
Console.WriteLine($"Authentication failed: {ex.Message}");
// Check IronPdf.log for detailed error information
}// Enable detailed logging
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "IronPdf.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Test authentication
try
{
var pdf = renderer.RenderUrlAsPdf("https://secure.example.com");
pdf.SaveAs("authenticated.pdf");
}
catch (Exception ex)
{
Console.WriteLine($"Authentication failed: {ex.Message}");
// Check IronPdf.log for detailed error information
}How Do I Login Using an HTML Form?
To log in by sending data to an HTML form, use the ChromeHttpLoginCredentials class. See IronPDF's ChromeHttpLoginCredentials API.
Consider these points:
- Post login data to the URL specified in the HTML form's ACTION attribute. Set this as the
LoginFormUrlattribute of HttpLoginCredentials. This may differ from the URL you want to render as a PDF. - Send data representing every input and textarea in the HTML form. The name attributes define each variable name (not the id).
- Some websites actively protect against machine login.
Here's a complete example of form-based authentication:
// Configure form-based login
var formLogin = new ChromeHttpLoginCredentials
{
LoginFormUrl = "https://example.com/login",
LoginFormData = new Dictionary<string, string>
{
{"username", "user@example.com"},
{"password", "securePassword123"},
{"rememberMe", "true"},
{"csrf_token", "abc123"} // Include any hidden fields
}
};
var renderer = new ChromePdfRenderer
{
LoginCredentials = formLogin,
RenderingOptions = new ChromePdfRenderOptions
{
RenderDelay = 3000, // Allow time for login redirect
EnableJavaScript = true
}
};
// The actual page you want to convert (after login)
var pdf = renderer.RenderUrlAsPdf("https://example.com/dashboard");
pdf.SaveAs("dashboard.pdf");// Configure form-based login
var formLogin = new ChromeHttpLoginCredentials
{
LoginFormUrl = "https://example.com/login",
LoginFormData = new Dictionary<string, string>
{
{"username", "user@example.com"},
{"password", "securePassword123"},
{"rememberMe", "true"},
{"csrf_token", "abc123"} // Include any hidden fields
}
};
var renderer = new ChromePdfRenderer
{
LoginCredentials = formLogin,
RenderingOptions = new ChromePdfRenderOptions
{
RenderDelay = 3000, // Allow time for login redirect
EnableJavaScript = true
}
};
// The actual page you want to convert (after login)
var pdf = renderer.RenderUrlAsPdf("https://example.com/dashboard");
pdf.SaveAs("dashboard.pdf");What Form Data Do I Need to Capture?
To authenticate successfully via HTML forms, capture all form inputs:
// Use this helper method to extract form fields
public Dictionary<string, string> ExtractFormFields(string loginPageHtml)
{
var formData = new Dictionary<string, string>();
var doc = new HtmlDocument();
doc.LoadHtml(loginPageHtml);
// Find all input fields
var inputs = doc.DocumentNode.SelectNodes("//input");
if (inputs != null)
{
foreach (var input in inputs)
{
string name = input.GetAttributeValue("name", "");
string value = input.GetAttributeValue("value", "");
string type = input.GetAttributeValue("type", "text");
if (!string.IsNullOrEmpty(name))
{
// Handle different input types
switch (type.ToLower())
{
case "checkbox":
if (input.Attributes["checked"] != null)
formData[name] = "on";
break;
case "radio":
if (input.Attributes["checked"] != null)
formData[name] = value;
break;
default:
formData[name] = value;
break;
}
}
}
}
// Don't forget select elements
var selects = doc.DocumentNode.SelectNodes("//select");
if (selects != null)
{
foreach (var select in selects)
{
string name = select.GetAttributeValue("name", "");
var selected = select.SelectSingleNode(".//option[@selected]");
if (selected != null && !string.IsNullOrEmpty(name))
{
formData[name] = selected.GetAttributeValue("value", "");
}
}
}
return formData;
}// Use this helper method to extract form fields
public Dictionary<string, string> ExtractFormFields(string loginPageHtml)
{
var formData = new Dictionary<string, string>();
var doc = new HtmlDocument();
doc.LoadHtml(loginPageHtml);
// Find all input fields
var inputs = doc.DocumentNode.SelectNodes("//input");
if (inputs != null)
{
foreach (var input in inputs)
{
string name = input.GetAttributeValue("name", "");
string value = input.GetAttributeValue("value", "");
string type = input.GetAttributeValue("type", "text");
if (!string.IsNullOrEmpty(name))
{
// Handle different input types
switch (type.ToLower())
{
case "checkbox":
if (input.Attributes["checked"] != null)
formData[name] = "on";
break;
case "radio":
if (input.Attributes["checked"] != null)
formData[name] = value;
break;
default:
formData[name] = value;
break;
}
}
}
}
// Don't forget select elements
var selects = doc.DocumentNode.SelectNodes("//select");
if (selects != null)
{
foreach (var select in selects)
{
string name = select.GetAttributeValue("name", "");
var selected = select.SelectSingleNode(".//option[@selected]");
if (selected != null && !string.IsNullOrEmpty(name))
{
formData[name] = selected.GetAttributeValue("value", "");
}
}
}
return formData;
}How Do I Find the Correct Form Action URL?
The form action URL is critical for successful authentication:
public string ExtractFormAction(string loginPageUrl, string loginPageHtml)
{
var doc = new HtmlDocument();
doc.LoadHtml(loginPageHtml);
// Find the login form
var form = doc.DocumentNode.SelectSingleNode("//form[contains(@action, 'login') or contains(@id, 'login') or contains(@class, 'login')]");
if (form == null)
{
// Try finding any form with password field
form = doc.DocumentNode.SelectSingleNode("//form[.//input[@type='password']]");
}
if (form != null)
{
string action = form.GetAttributeValue("action", "");
// Resolve relative URLs
if (!string.IsNullOrEmpty(action))
{
var baseUri = new Uri(loginPageUrl);
var actionUri = new Uri(baseUri, action);
return actionUri.ToString();
}
}
// Default to the login page URL if no action found
return loginPageUrl;
}public string ExtractFormAction(string loginPageUrl, string loginPageHtml)
{
var doc = new HtmlDocument();
doc.LoadHtml(loginPageHtml);
// Find the login form
var form = doc.DocumentNode.SelectSingleNode("//form[contains(@action, 'login') or contains(@id, 'login') or contains(@class, 'login')]");
if (form == null)
{
// Try finding any form with password field
form = doc.DocumentNode.SelectSingleNode("//form[.//input[@type='password']]");
}
if (form != null)
{
string action = form.GetAttributeValue("action", "");
// Resolve relative URLs
if (!string.IsNullOrEmpty(action))
{
var baseUri = new Uri(loginPageUrl);
var actionUri = new Uri(baseUri, action);
return actionUri.ToString();
}
}
// Default to the login page URL if no action found
return loginPageUrl;
}What Are Common Issues with Form-Based Authentication?
- CSRF Tokens: Many forms include anti-forgery tokens that expire
- JavaScript Validation: Some forms require JavaScript execution
- Multi-Step Authentication: Forms that require multiple pages
- CAPTCHA Protection: Human verification challenges
- Session Timeouts: Login sessions that expire quickly
How Do I Handle Anti-Bot Protection?
Modern websites implement protection against automated logins:
// Strategies for handling anti-bot measures
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
// Mimic real browser behavior
ViewPortWidth = 1920,
ViewPortHeight = 1080,
EnableJavaScript = true,
RenderDelay = 5000, // Wait for JavaScript
// Set a real user agent
CustomHttpHeaders = new Dictionary<string, string>
{
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
}
}
};
// For sites with rate limiting, add delays between requests
System.Threading.Thread.Sleep(2000);// Strategies for handling anti-bot measures
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
// Mimic real browser behavior
ViewPortWidth = 1920,
ViewPortHeight = 1080,
EnableJavaScript = true,
RenderDelay = 5000, // Wait for JavaScript
// Set a real user agent
CustomHttpHeaders = new Dictionary<string, string>
{
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
}
}
};
// For sites with rate limiting, add delays between requests
System.Threading.Thread.Sleep(2000);For comprehensive HTML to PDF conversion guidance, including complex authentication scenarios, visit the HTML to PDF tutorial.
How Do I Handle MVC Authentication?
The following workaround renders a .NET MVC view to a string programmatically, avoiding MVC logins while rendering views faithfully. This approach works well when converting CSHTML to PDF in MVC Core or MVC Framework.
// Converts an MVC partial view to a string
public static string RenderPartialViewToString(this Controller controller, string viewPath, object model = null)
{
try
{
// Set the model
var context = controller.ControllerContext;
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
// Find the partial view
var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath);
if (viewResult.View == null)
{
throw new Exception($"Partial view {viewPath} could not be found.");
}
// Create a view context
var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
// Render the view
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(context, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
catch (Exception ex)
{
// Return error message if there is an exception
return ex.Message;
}
}
// Usage in an MVC Controller
public ActionResult GeneratePdf()
{
// Render authenticated view to string
var model = new InvoiceViewModel { /* populate model */ };
string html = this.RenderPartialViewToString("~/Views/Invoice/Details.cshtml", model);
// Convert to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
// Return PDF file
return File(pdf.BinaryData, "application/pdf", "invoice.pdf");
}// Converts an MVC partial view to a string
public static string RenderPartialViewToString(this Controller controller, string viewPath, object model = null)
{
try
{
// Set the model
var context = controller.ControllerContext;
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
// Find the partial view
var viewResult = ViewEngines.Engines.FindPartialView(context, viewPath);
if (viewResult.View == null)
{
throw new Exception($"Partial view {viewPath} could not be found.");
}
// Create a view context
var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
// Render the view
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(context, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
catch (Exception ex)
{
// Return error message if there is an exception
return ex.Message;
}
}
// Usage in an MVC Controller
public ActionResult GeneratePdf()
{
// Render authenticated view to string
var model = new InvoiceViewModel { /* populate model */ };
string html = this.RenderPartialViewToString("~/Views/Invoice/Details.cshtml", model);
// Convert to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
// Return PDF file
return File(pdf.BinaryData, "application/pdf", "invoice.pdf");
}Why Render Views to String Instead of Direct Conversion?
Rendering MVC views to string provides several key advantages:
- Authentication Context: Views render within the authenticated user's context
- Full MVC Pipeline: All MVC features work including
ViewBag,TempData, andHtmlhelpers - Layout Support: Master pages and layouts render correctly
- Model Binding: Complex view models work seamlessly
- Action Filters: Security and logging filters execute normally
What Are the Benefits of This MVC Workaround?
The MVC string rendering approach offers:
- Security: No need to expose internal URLs or bypass authentication
- Performance: Avoid additional HTTP requests
- Consistency: Identical output to what users see in browsers
- Flexibility: Modify HTML before PDF conversion
- Testing: Unit test HTML generation easily
How Do I Pass Models to the Rendered View?
Here's a comprehensive example with a complex model:
public class InvoiceController : Controller
{
private readonly IInvoiceService _invoiceService;
public async Task<ActionResult> DownloadInvoicePdf(int invoiceId)
{
// Load data within authenticated context
var invoice = await _invoiceService.GetInvoiceAsync(invoiceId);
if (invoice == null || invoice.UserId != User.Identity.GetUserId())
{
return HttpNotFound();
}
// Create view model
var viewModel = new InvoiceDetailsViewModel
{
Invoice = invoice,
Company = await _invoiceService.GetCompanyDetailsAsync(),
LineItems = await _invoiceService.GetLineItemsAsync(invoiceId),
TaxDetails = await _invoiceService.GetTaxDetailsAsync(invoiceId)
};
// Render to HTML string
string html = this.RenderPartialViewToString("~/Views/Invoice/DetailsPdf.cshtml", viewModel);
// Add custom styling for PDF
html = $@"
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; }}
.invoice-header {{ background-color: #f0f0f0; padding: 20px; }}
.line-items {{ width: 100%; border-collapse: collapse; }}
.line-items th, .line-items td {{ border: 1px solid #ddd; padding: 8px; }}
</style>
</head>
<body>
{html}
</body>
</html>";
// Convert to PDF with options
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
MarginTop = 20,
MarginBottom = 20,
MarginLeft = 10,
MarginRight = 10,
PrintHtmlBackgrounds = true
}
};
var pdf = renderer.RenderHtmlAsPdf(html);
// Add metadata
pdf.MetaData.Author = "Invoice System";
pdf.MetaData.Title = $"Invoice #{invoice.Number}";
pdf.MetaData.CreationDate = DateTime.Now;
return File(pdf.BinaryData, "application/pdf", $"Invoice-{invoice.Number}.pdf");
}
}public class InvoiceController : Controller
{
private readonly IInvoiceService _invoiceService;
public async Task<ActionResult> DownloadInvoicePdf(int invoiceId)
{
// Load data within authenticated context
var invoice = await _invoiceService.GetInvoiceAsync(invoiceId);
if (invoice == null || invoice.UserId != User.Identity.GetUserId())
{
return HttpNotFound();
}
// Create view model
var viewModel = new InvoiceDetailsViewModel
{
Invoice = invoice,
Company = await _invoiceService.GetCompanyDetailsAsync(),
LineItems = await _invoiceService.GetLineItemsAsync(invoiceId),
TaxDetails = await _invoiceService.GetTaxDetailsAsync(invoiceId)
};
// Render to HTML string
string html = this.RenderPartialViewToString("~/Views/Invoice/DetailsPdf.cshtml", viewModel);
// Add custom styling for PDF
html = $@"
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; }}
.invoice-header {{ background-color: #f0f0f0; padding: 20px; }}
.line-items {{ width: 100%; border-collapse: collapse; }}
.line-items th, .line-items td {{ border: 1px solid #ddd; padding: 8px; }}
</style>
</head>
<body>
{html}
</body>
</html>";
// Convert to PDF with options
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
MarginTop = 20,
MarginBottom = 20,
MarginLeft = 10,
MarginRight = 10,
PrintHtmlBackgrounds = true
}
};
var pdf = renderer.RenderHtmlAsPdf(html);
// Add metadata
pdf.MetaData.Author = "Invoice System";
pdf.MetaData.Title = $"Invoice #{invoice.Number}";
pdf.MetaData.CreationDate = DateTime.Now;
return File(pdf.BinaryData, "application/pdf", $"Invoice-{invoice.Number}.pdf");
}
}Before implementing any authentication solution, ensure you have installed IronPDF properly and configured your license keys.
Ready to see what else you can do? Visit our tutorial page: Convert PDFs.
Frequently Asked Questions
How can I convert HTML to PDF when the content is behind a login form?
IronPDF provides multiple approaches for converting HTML to PDF behind login authentication. You can use the ChromeHttpLoginCredentials API for TLS network authentication, or download the HTML content using System.Net.WebClient or HttpClient with appropriate authentication headers before converting it to PDF with IronPDF.
What is ChromeHttpLoginCredentials and how do I use it?
ChromeHttpLoginCredentials is IronPDF's API for handling network authentication. You can use it by setting the LoginCredentials property on ChromePdfRenderer with your username and password, allowing IronPDF to automatically authenticate when rendering password-protected URLs to PDF.
Can I handle HTML form-based logins for PDF conversion?
Yes, IronPDF supports HTML form-based logins. The recommended approach is to use System.Net.WebClient or HttpClient to handle the login process, download the authenticated HTML content, and then use IronPDF's RenderHtmlAsPdf method to convert the downloaded HTML to PDF.
How do I download HTML assets like images and stylesheets from authenticated pages?
You can use HtmlAgilityPack to parse the downloaded HTML and extract asset URLs like images and stylesheets. Then use System.Net.WebClient to download each asset with the same authentication headers before converting the complete HTML package to PDF with IronPDF.
What's the best practice for handling authentication tokens or headers?
When using IronPDF with authentication tokens, download the HTML using HttpClient or WebClient with your authentication headers (such as Bearer tokens). Once you have the authenticated HTML content in memory or saved to disk, use IronPDF's ChromePdfRenderer to convert it to PDF.
Is there a workaround for MVC login authentication?
Yes, IronPDF provides workarounds for MVC login authentication scenarios. The recommended approach is to first authenticate and download the HTML content using standard .NET HTTP clients, then pass the authenticated HTML directly to IronPDF's rendering engine rather than having IronPDF handle the authentication.






