How to Convert Webpage to PDF in ASP .NET using C# and IronPDF
Converting web pages and HTML pages to PDF documents in ASP.NET applications is a common requirement for generating reports, invoices, and downloadable content. IronPDF is a .NET library that provides a simple yet powerful solution to convert webpage to PDF in ASP.NET using C#, converting input HTML, web pages, and HTML files into professional PDF files with just a few lines of code.
This is essential for any modern .NET project. So be sure to follow along as we look into different methods for converting your HTML and web content into high-quality PDF documents!
Getting Started with IronPDF
To begin converting web pages to PDF in your ASP.NET application, install IronPDF through the NuGet Package Manager Console:
Install-Package IronPdf
After installation, add the using statement to your C# files:
using IronPdf;using IronPdf;Imports IronPdfIronPDF works seamlessly with both ASP.NET Web Forms and ASP.NET Core applications, providing consistent HTML to PDF conversion across all .NET frameworks. For deployment, consider using it with an Azure Function for serverless scaling.
How to Convert HTML String to PDF?
The ChromePdfRenderer class is IronPDF's primary PDF converter for transforming HTML content into PDF documents. This method is perfect for generating PDFs from dynamically created HTML strings:
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Create the PDF converter
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF
string htmlContent = "<h1>Sales Report</h1><p>Quarterly sales data...</p>";
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF file
pdfDocument.SaveAs("report.pdf");
}
}using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Create the PDF converter
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF
string htmlContent = "<h1>Sales Report</h1><p>Quarterly sales data...</p>";
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF file
pdfDocument.SaveAs("report.pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comHTML to PDF File Output

The RenderHtmlAsPdf method preserves all CSS styles, images, and HTML elements in your PDF document. When working with HTML code that references load external resource (like CSS or images), specify a base path:
var renderer = new ChromePdfRenderer();
string html = @"
<link rel='stylesheet' href='styles.css'>
<h1>Company Report</h1>
<div style='text-align: center'>Annual Summary</div>";
// Set base path for external resources
var pdf = renderer.RenderHtmlAsPdf(html, @"C:\Assets\");
pdf.SaveAs("annual-report.pdf");var renderer = new ChromePdfRenderer();
string html = @"
<link rel='stylesheet' href='styles.css'>
<h1>Company Report</h1>
<div style='text-align: center'>Annual Summary</div>";
// Set base path for external resources
var pdf = renderer.RenderHtmlAsPdf(html, @"C:\Assets\");
pdf.SaveAs("annual-report.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comHow to Convert Current Webpage to PDF in ASP.NET?
For ASP.NET Web Forms applications, IronPDF offers an incredibly simple one-line solution to convert webpages directly to PDF:
protected void Page_Load(object sender, EventArgs e)
{
// Convert current ASPX page to PDF
IronPdf.AspxToPdf.RenderThisPageAsPdf();
}protected void Page_Load(object sender, EventArgs e)
{
// Convert current ASPX page to PDF
IronPdf.AspxToPdf.RenderThisPageAsPdf();
}IRON VB CONVERTER ERROR developers@ironsoftware.comASPX to PDF Output

This single line of code converts the entire current webpage with all its HTML structure, styles, and dynamic content into a PDF file, viewable in Adobe Reader or other PDF viewers. You can customize the output behavior:
// Display PDF directly in browser
IronPdf.AspxToPdf.RenderThisPageAsPdf(
IronPdf.AspxToPdf.FileBehavior.InBrowser,
"invoice.pdf"
);// Display PDF directly in browser
IronPdf.AspxToPdf.RenderThisPageAsPdf(
IronPdf.AspxToPdf.FileBehavior.InBrowser,
"invoice.pdf"
);IRON VB CONVERTER ERROR developers@ironsoftware.comFor ASP.NET Core MVC applications, use the ChromePdfRenderer to convert HTML views. You'll typically work within Visual Studio for this .NET project:
public IActionResult GeneratePdf()
{
try
{
// 1. Initialize the PDF Renderer
var renderer = new IronPdf.ChromePdfRenderer();
// 2. Construct the URL that IronPDF will convert.
// This URL calls the 'Report' action above.
string url = $"{Request.Scheme}://{Request.Host}/Home/Report";
// Optional: Customize Rendering Options (e.g., margins)
renderer.RenderingOptions.MarginTop = 15;
renderer.RenderingOptions.MarginBottom = 15;
renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
CenterText = "{page} of {total-pages}",
FontSize = 10
};
// 3. Render the URL as a PDF
var pdf = renderer.RenderUrlAsPdf(url);
// 4. Send the PDF file to the browser
// The user will be prompted to download 'report.pdf'
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
catch (Exception ex)
{
// Log the error and return a generic error page or message
// Console.WriteLine(ex.Message);
return Content($"Error generating PDF: {ex.Message}");
}
}public IActionResult GeneratePdf()
{
try
{
// 1. Initialize the PDF Renderer
var renderer = new IronPdf.ChromePdfRenderer();
// 2. Construct the URL that IronPDF will convert.
// This URL calls the 'Report' action above.
string url = $"{Request.Scheme}://{Request.Host}/Home/Report";
// Optional: Customize Rendering Options (e.g., margins)
renderer.RenderingOptions.MarginTop = 15;
renderer.RenderingOptions.MarginBottom = 15;
renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
CenterText = "{page} of {total-pages}",
FontSize = 10
};
// 3. Render the URL as a PDF
var pdf = renderer.RenderUrlAsPdf(url);
// 4. Send the PDF file to the browser
// The user will be prompted to download 'report.pdf'
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
catch (Exception ex)
{
// Log the error and return a generic error page or message
// Console.WriteLine(ex.Message);
return Content($"Error generating PDF: {ex.Message}");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comASP.NET MVC Output

How to Convert URL to PDF?
IronPDF can easily convert HTML from any URL into a PDF document. This powerful feature works with both external websites and internal application pages:
var renderer = new ChromePdfRenderer();
// Convert external webpage to PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("website.pdf");var renderer = new ChromePdfRenderer();
// Convert external webpage to PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("website.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comOutput

For pages with JavaScript or dynamic content, you might use headless mode or enable JavaScript execution:
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay(1000); // Wait for JS to load
var pdf = renderer.RenderUrlAsPdf("https://app.example.com/dashboard");
pdf.SaveAs("dashboard.pdf");var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay(1000); // Wait for JS to load
var pdf = renderer.RenderUrlAsPdf("https://app.example.com/dashboard");
pdf.SaveAs("dashboard.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comHow to Configure PDF Generation Settings?
IronPDF provides extensive customization options for PDF conversion to match your specific requirements:
var renderer = new ChromePdfRenderer();
// Configure page layout
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape;
// Set margins in millimeters
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.MarginLeft = 15;
renderer.RenderingOptions.MarginRight = 15;
// Enable background colors and images
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Generate PDF with custom settings
var pdf = renderer.RenderHtmlAsPdf("<h1>Custom Layout</h1>");
pdf.SaveAs("custom.pdf");var renderer = new ChromePdfRenderer();
// Configure page layout
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Landscape;
// Set margins in millimeters
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.MarginLeft = 15;
renderer.RenderingOptions.MarginRight = 15;
// Enable background colors and images
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Generate PDF with custom settings
var pdf = renderer.RenderHtmlAsPdf("<h1>Custom Layout</h1>");
pdf.SaveAs("custom.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comOutput

How to Add Headers and Footers to PDF Files?
Professional PDF documents often require headers and footers with page numbers and other information:
var renderer = new ChromePdfRenderer();
// Add text header and footer
renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "Monthly Report",
DrawDividerLine = true,
FontSize = 16
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
FontSize = 10
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Report Content</h1>");
pdf.SaveAs("report-with-header.pdf");var renderer = new ChromePdfRenderer();
// Add text header and footer
renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "Monthly Report",
DrawDividerLine = true,
FontSize = 16
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
FontSize = 10
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Report Content</h1>");
pdf.SaveAs("report-with-header.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comOutput

For more complex headers, use HTML formatting:
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center; border-bottom: 1px solid #ccc;'>" +
"<h2>Company Name</h2></div>",
MaxHeight = 30
};renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center; border-bottom: 1px solid #ccc;'>" +
"<h2>Company Name</h2></div>",
MaxHeight = 30
};IRON VB CONVERTER ERROR developers@ironsoftware.comWorking with Different HTML Sources
IronPDF provides multiple methods to generate PDFs from various HTML sources:
Converting HTML Files
var renderer = new ChromePdfRenderer();
// Convert HTML file to PDF
var pdf = renderer.RenderHtmlFileAsPdf(@"C:\Templates\invoice.html");var renderer = new ChromePdfRenderer();
// Convert HTML file to PDF
var pdf = renderer.RenderHtmlFileAsPdf(@"C:\Templates\invoice.html");IRON VB CONVERTER ERROR developers@ironsoftware.comConverting with Base64 Images
string htmlWithImage = @"
<h1>Product Catalog</h1>
<img src='data:image/png;base64,iVBORw0KGgoAAAANS...' />";
var pdf = renderer.RenderHtmlAsPdf(htmlWithImage);string htmlWithImage = @"
<h1>Product Catalog</h1>
<img src='data:image/png;base64,iVBORw0KGgoAAAANS...' />";
var pdf = renderer.RenderHtmlAsPdf(htmlWithImage);IRON VB CONVERTER ERROR developers@ironsoftware.comConclusion
IronPDF makes converting HTML to PDF in ASP.NET applications remarkably straightforward. Whether you need to generate PDF documents from HTML strings, convert HTML files, or transform entire web pages into PDFs, IronPDF provides a robust HTML to PDF converter that preserves all HTML content, CSS styles, and JavaScript functionality. It's suitable for large-scale applications as well as lightweight tools like those used in mobile apps or a simple command line utility.
The PDF library handles everything from simple HTML documents to complex web content, making it the ideal choice for PDF conversion in your .NET applications. With support for headers, footers, security settings, and various HTML sources, you can create PDF documents that meet professional standards with minimal code.
Get started with IronPDF today through a free trial and experience seamless HTML to PDF conversion in your ASP.NET projects. For production use, explore our flexible licensing options that scale with your needs.
Frequently Asked Questions
How can I convert a webpage to PDF in ASP.NET?
You can use IronPDF, a .NET library, to convert webpages to PDF in ASP.NET applications. It allows you to generate professional PDF files from HTML content with just a few lines of C# code.
What are the typical use cases for converting HTML to PDF in ASP.NET?
Common use cases include generating reports, creating invoices, and providing downloadable content in a PDF format from web pages within ASP.NET applications.
Is IronPDF suitable for generating professional-grade PDFs?
Yes, IronPDF is designed to convert HTML and web pages into high-quality, professional PDF documents, making it suitable for business and enterprise applications.
How easy is it to use IronPDF in an ASP.NET application?
IronPDF is very user-friendly. It allows developers to convert HTML to PDF with minimal code, making it easy to integrate into existing ASP.NET applications.
Can IronPDF handle complex web pages with CSS and JavaScript?
Yes, IronPDF can process complex web pages that include CSS and JavaScript, ensuring that the resulting PDF maintains the look and feel of the original webpage.
Is it possible to automate PDF generation from web pages using IronPDF?
Absolutely. IronPDF can be integrated into automated workflows to generate PDFs from web pages programmatically in ASP.NET applications.
Does IronPDF support converting HTML files directly to PDF?
Yes, IronPDF allows you to convert not only web pages but also local HTML files directly into PDF documents.
What programming language is used with IronPDF for PDF conversion?
IronPDF uses C# for converting HTML and web pages to PDF in ASP.NET applications.
Is IronPDF capable of generating PDFs from dynamic web content?
Yes, IronPDF can handle dynamic web content, ensuring that the generated PDFs reflect the latest version of the web page at the time of conversion.
What are the benefits of using IronPDF for HTML to PDF conversion?
IronPDF provides a robust and straightforward way to convert HTML to PDF, offering benefits such as ease of use, high-quality output, support for complex layouts, and automation capabilities.









