How to Convert HTML String to PDF in C#
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 SecondsTransform 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.
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
IronPdf.ChromePdfRenderer.StaticRenderHtmlAsPdf("<p>Hello World</p>").SaveAs("string-to-pdf.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)

- Download IronPDF C# Library from NuGet
- Instantiate the PDF Renderer and Pass the HTML String
- Configure BasePath for External Assets in PDF
- Configure the RenderingOptions to fine-tune the output PDF
- Save and Download the Generated PDF
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");Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Create a PDF from a HTML string using C#
Private 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.
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;' Enable web security before rendering untrusted HTML
IronPdf.Installation.EnableWebSecurity = TrueFor 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");Dim styledHtml As String = "
<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 styledPdf = renderer.RenderHtmlAsPdf(styledHtml)
styledPdf.SaveAs("styled-output.pdf")
End UsingWhat 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.
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.
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");Imports IronPdf
' Instantiate Renderer
Private 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
Private 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");Imports System.IO
' Example with JavaScript-generated content
Dim jsHtml As String = "
<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 dynamicPdf = renderer.RenderHtmlAsPdf(jsHtml, "C:\site\")
dynamicPdf.SaveAs("dynamic-content.pdf")
End UsingWhen 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:
- Converting HTML that references local file system resources
- Working with content management systems that use relative URLs
- Migrating existing web content to PDF format
- 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"
);' Using a web URL as base path
Dim 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 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.