IRONSOFTWAREHOME

How to Convert HTML String to PDF in C#

Curtis Chau
Curtis Chau
Updated: August 2, 2026

IronPDF converts HTML strings to PDF documents in C# using a Chrome rendering engine, supporting all HTML5, CSS3, and JavaScript content with a single line of code for basic conversions. The library provides powerful features for creating PDFs while maintaining perfect rendering fidelity.

IronPDF allows developers to create PDF documents easily in C#, F#, and VB.NET for .NET Core and .NET Framework. IronPDF supports rendering any HTML string into a PDF, and the rendering process uses a fully functional version of the Google Chromium engine. This ensures HTML content appears exactly as it would in a modern web browser, making it ideal for generating reports, invoices, and documents from dynamic HTML content.

Quickstart: Convert HTML String to PDF in Seconds

Transform HTML strings into PDF files using IronPDF. This guide demonstrates how to convert an HTML string to a PDF document in C# with minimal code. Perfect for developers who need to integrate PDF rendering capabilities into their projects.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy and run this code snippet.

    IronPdf.ChromePdfRenderer.StaticRenderHtmlAsPdf("<p>Hello World</p>").SaveAs("string-to-pdf.pdf");
    C#
  3. 3Deploy to test on your live environment

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

How Do I Convert a Simple HTML String to PDF?

Here's an example of IronPDF rendering an HTML string into a PDF using the RenderHtmlAsPdf method. The parameter is an HTML string to be rendered as a PDF. This method is part of the ChromePdfRenderer class, which provides extensive control over the rendering options for your PDF generation needs.

using IronPdf;

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

// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or Stream
pdf.SaveAs("output.pdf");

The RenderHtmlAsPdf method returns a PdfDocument object, which holds PDF information and provides methods for manipulating the PDF, including merging PDFs, adding watermarks, and setting security options.

Tips: In ASP.NET Core, Blazor, or background services, prefer the async variant RenderHtmlAsPdfAsync to avoid blocking threads. The PdfDocument returned implements IDisposable, so wrap it in a using block for high-throughput scenarios. ChromePdfRenderer is thread-safe and can be instantiated once and reused across requests for better performance.

When an HTML string is obtained from an external source and you need to disable local disk access or cross-origin requests, set IronPdf.Installation.EnableWebSecurity to true before rendering. This is a static global setting, not an instance property on the renderer.

// Enable web security before rendering untrusted HTML
IronPdf.Installation.EnableWebSecurity = true;

For complex HTML content that includes CSS styling, enhance your HTML strings with inline styles or reference external stylesheets using the Base URLs & Asset Encoding feature:

// Example with inline CSS
var styledHtml = @"
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #333; border-bottom: 2px solid #0066cc; }
        p { line-height: 1.6; }
    </style>
</head>
<body>
    <h1>Professional Report</h1>
    <p>This PDF was generated from HTML with custom styling.</p>
</body>
</html>";

using var styledPdf = renderer.RenderHtmlAsPdf(styledHtml);
styledPdf.SaveAs("styled-output.pdf");

What Does the Generated PDF Look Like?

This is the file that the code produced:

The generated PDF maintains the exact formatting and styling from your HTML, making it perfect for generating PDF reports or converting web content to print-ready documents.

My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.

David Jones

Lead Software Engineer, Agorus Build

View case study

How Can I Include External Assets When Converting HTML to PDF?

This example shows IronPDF loading an external image asset from an optional BasePath. Setting the BaseUrlOrPath property gives the relative file path or URL context for hyperlinks, images, CSS, and JavaScript files. This is essential when working with images in PDFs or when you need to reference external resources.

using IronPdf;

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

// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");

For advanced scenarios involving complex layouts or dynamic content, utilize IronPDF's support for JavaScript rendering:

// Example with JavaScript-generated content
var jsHtml = @"
<html>
<head>
    <script>
        window.onload = function() {
            document.getElementById('dynamic').innerHTML = 
                'Generated on: ' + new Date().toLocaleString();
        };
    </script>
</head>
<body>
    <h1>Dynamic Content Example</h1>
    <div id='dynamic'></div>
</body>
</html>";

// Configure rendering to wait for JavaScript execution
renderer.RenderingOptions.WaitFor.RenderDelay(500); // milliseconds
using var dynamicPdf = renderer.RenderHtmlAsPdf(jsHtml, @"C:\site\");
dynamicPdf.SaveAs("dynamic-content.pdf");

When working with external assets, you might need to handle authentication or special headers. IronPDF supports this through the HTTP Request Header functionality, allowing you to include authorization tokens or custom headers when fetching resources.

Why Is Setting BasePath Important for External Resources?

This is the file that the code produced:

Setting the BasePath correctly ensures all relative references in your HTML are resolved properly. Without it, IronPDF cannot locate external resources like images, stylesheets, or scripts. This is particularly important when:

  1. Converting HTML that references local file system resources
  2. Working with content management systems that use relative URLs
  3. Migrating existing web content to PDF format
  4. Creating templates that use shared assets

For web-based resources, use a full URL as the base path:

// Using a web URL as base path
var webBasedPdf = renderer.RenderHtmlAsPdf(
    "<link rel='stylesheet' href='/styles/main.css'><h1>Web Content</h1>", 
    "https://mywebsite.com"
);

For additional control over the PDF generation process, explore IronPDF's extensive documentation on custom margins, page orientation, and PDF compression to optimize your output files for various use cases.

Frequently Asked Questions

How can I convert an HTML string to a PDF using IronPDF in C#?

To convert an HTML string to a PDF using IronPDF in C#, you can use the `RenderHtmlAsPdf` method of the `ChromePdfRenderer` class. This method supports full HTML5, CSS3, and JavaScript and converts the HTML into a PDF document with perfect rendering fidelity.

Is it possible to include external assets like images and CSS when converting HTML to PDF with IronPDF?

Yes, you can include external assets by setting the `BaseUrlOrPath` property in IronPDF. This property allows IronPDF to resolve references like images, CSS, and JavaScript during the PDF conversion process.

Does IronPDF support JavaScript execution during HTML to PDF conversion?

IronPDF fully supports JavaScript rendering during the conversion process. You can configure the rendering options to wait for JavaScript execution by setting the render delay in the `RenderingOptions`.

How can I enhance the styling of my HTML before converting it to a PDF with IronPDF?

You can enhance the styling of your HTML by using inline CSS or referencing external stylesheets in your HTML string. IronPDF will render the styles as they appear in a modern browser.

Can IronPDF handle rendering HTML from dynamic content sources?

Yes, IronPDF can handle HTML from dynamic content sources, making it ideal for generating documents such as reports and invoices from web content.

What are the benefits of using IronPDF for rendering HTML to PDF?

IronPDF provides high-fidelity rendering using the Chrome engine, supports all modern HTML, CSS, and JavaScript, and offers extensive customization options such as margins, orientation, and security.

Does IronPDF offer features for securing and customizing the generated PDF files?

Yes, IronPDF offers various features for securing PDFs, such as password protection and setting permissions. It also allows customization like adding watermarks and merged document creation.

Is IronPDF suitable for use in high-performance environments?

IronPDF is suitable for high-performance environments. It is thread-safe and its `RenderHtmlAsPdf` method can be wrapped in a `using` block due to its implementation of `IDisposable`, ensuring efficient resource management.

How can I ensure my external web resources are correctly referenced in IronPDF?

To ensure external web resources are correctly referenced, specify the correct `BasePath` or use a full URL as the base path for web-based content. This includes images, stylesheets, and scripts.

What is the role of `ChromePdfRenderer` in IronPDF?

The `ChromePdfRenderer` in IronPDF is responsible for converting HTML to PDF. It uses a fully functional version of the Google Chromium engine to ensure accurate rendering as seen in a web browser.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,062,892Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999