HTML String to PDF
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");

// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BaseUrlOrPath '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");
NuGet Download
PM > Install-Package IronPdf
HTML String to PDF

HTML String to PDF

With IronPDF, you can create new PDF documents from HTML strings within a .NET project, and IronPDF can be used from C#, F# and VB.NET. The ChromePdfRenderer class renders the HTML with a built-in Chromium engine, and the guide to pixel-perfect HTML to PDF explains how to match the output to the browser. The HTML to PDF tutorial covers the wider set of conversion features.

The first step to converting an HTML string to a PDF in C# is to install the IronPDF library in the project. The using IronPdf directive at the top of the example gives access to the classes needed for HTML to PDF conversion.

The example creates an instance of ChromePdfRenderer, which handles the conversion of HTML to PDF. The RenderHtmlAsPdf method converts a simple HTML string ("<h1>Hello World</h1>") into a PDF document, and the SaveAs method writes that document to disk.

In the advanced example, IronPDF renders HTML that refers to external assets such as images, CSS and JavaScript files. The optional BaseUrlOrPath argument of RenderHtmlAsPdf sets the folder or URL against which relative paths resolve, so icons/iron.png loads from the icons folder under that base path. The resulting PDF, which includes the external assets, is saved with the same method. For paper size, margins, headers, footers and other output settings, see the guide to rendering options.

Warning: Set Installation.EnableWebSecurity = true when rendering untrusted HTML from users. Web security disables operations that require local disk access or cross-origin requests, so local assets, such as the image in the advanced example, do not load.
Learn to Convert HTML Strings to PDF in C# with IronPDF
View on GitHub

Ready to Get Started?

Nuget Downloads 21,916,531Version:2026.11just released