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 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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
IronPdf.ChromePdfRender.StaticRenderHtmlAsPdf("<p>Hello World</p>").SaveAs("string-to-pdf.pdf");Deploy to test on your live environment
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.
:path=/static-assets/pdf/content-code-examples/how-to/html-string-to-pdf.csusing 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 is a class used to hold PDF information. This object provides numerous methods for manipulating the PDF, including merging PDFs, adding watermarks, and setting security options.
When an HTML string is obtained from an external source and you need to disable local disk access or cross-origin requests, set the ChromePdfRenderer.EnableWebSecurity property to 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>";
var styledPdf = renderer.RenderHtmlAsPdf(styledHtml);
styledPdf.SaveAs("styled-output.pdf");// 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>";
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.
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 feature is essential when working with images in PDFs or when you need to reference external resources.
:path=/static-assets/pdf/content-code-examples/how-to/html-string-to-pdf-2.csusing 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
var dynamicPdf = renderer.RenderHtmlAsPdf(jsHtml, @"C:\site\");
dynamicPdf.SaveAs("dynamic-content.pdf");// 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
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:
- 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
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 do I convert a simple HTML string to PDF in C#?
You can convert an HTML string to PDF using IronPDF's ChromePdfRenderer class with the RenderHtmlAsPdf method. Simply instantiate the renderer, pass your HTML string to the method, and save the resulting PdfDocument object. IronPDF uses a Chrome rendering engine to ensure perfect fidelity.
What rendering engine does the HTML to PDF conversion use?
IronPDF uses a fully functional version of the Google Chromium engine for rendering HTML to PDF. This ensures that your HTML content, including HTML5, CSS3, and JavaScript, appears exactly as it would in a modern web browser.
Can I convert HTML with CSS styling to PDF?
Yes, IronPDF supports converting HTML with CSS styling to PDF. You can use inline styles directly in your HTML string or reference external stylesheets using the Base URLs & Asset Encoding feature to ensure proper rendering of styled content.
What is the quickest way to convert HTML to PDF?
The quickest way is using IronPDF's static method: IronPdf.ChromePdfRenderer.StaticRenderHtmlAsPdf("Your HTML").SaveAs("output.pdf"). This single line of code converts your HTML string and saves it as a PDF file.
How do I handle external assets when converting HTML strings?
IronPDF allows you to configure the BasePath for external assets in your PDF. This ensures that images, stylesheets, and other resources referenced in your HTML are properly loaded during the conversion process.
Can I customize the PDF output when converting from HTML?
Yes, IronPDF provides extensive RenderingOptions to fine-tune your PDF output. You can control page size, margins, headers, footers, and many other aspects of the generated PDF through the ChromePdfRenderer class.
What can I do with the generated PDF after conversion?
IronPDF's PdfDocument object provides numerous methods for PDF manipulation, including merging PDFs, adding watermarks, setting security options, and more. The converted PDF can be saved to disk, streamed, or further processed as needed.
How do I handle security when converting HTML from external sources?
When converting HTML from external sources, IronPDF allows you to set the EnableWebSecurity property to true on the ChromePdfRenderer. This disables local disk access and cross-origin requests for enhanced security.






