How to Convert HTML String to PDF in C#
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 is undertaken by a fully functional version of the Google Chromium engine.
Convert an HTML String into a PDF in seconds!
IronPdf.ChromePdfRender
.StaticRenderHtmlAsPdf("<p>Hello Word</p>")
.SaveAs("string-to-pdf.pdf");
How to Render HTML String to PDF

- 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
HTML String to PDF Example
Here we have an example of IronPDF rendering an HTML string into a PDF by using the RenderHtmlAsPdf
method. The parameter is an HTML string to be rendered as a PDF.
:path=/static-assets/pdf/content-code-examples/how-to/html-string-to-pdf.cs
using IronPdf; // Import the IronPdf library to use its PDF rendering capabilities
// Instantiate a PDF renderer using the ChromePdfRenderer class
var renderer = new ChromePdfRenderer();
// Render a PDF from a simple HTML string containing a heading
// The RenderHtmlAsPdf method is called on the renderer object, converting the HTML string into a PDF document
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Save the produced PDF document to a file on the filesystem
// The SaveAs method writes the PDF content to a file specified by the path
pdf.SaveAs("output.pdf");
Imports IronPdf ' Import the IronPdf library to use its PDF rendering capabilities
' Instantiate a PDF renderer using the ChromePdfRenderer class
Private renderer = New ChromePdfRenderer()
' Render a PDF from a simple HTML string containing a heading
' The RenderHtmlAsPdf method is called on the renderer object, converting the HTML string into a PDF document
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
' Save the produced PDF document to a file on the filesystem
' The SaveAs method writes the PDF content to a file specified by the path
pdf.SaveAs("output.pdf")
The RenderHtmlAsPdf
method returns a PdfDocument
object, which is a class used to hold PDF information.
In cases where an HTML string is obtained from an external source, and disabling local disk access or cross-origin requests is desired, the ChromePdfRenderer.EnableWebSecurity
property can be set to true to achieve that.
Result
This is the file that the code produced:
Advanced HTML to PDF Example
Here we have an example of 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.
:path=/static-assets/pdf/content-code-examples/how-to/html-string-to-pdf-2.cs
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
/*
* This code uses the IronPdf library to create a PDF from HTML content.
* The ChromePdfRenderer provides a simple interface for rendering HTML and DOM as PDF.
*/
// Advanced Example with HTML Assets
/*
* The following example demonstrates how to render a PDF from HTML containing external assets
* such as images, CSS, and JavaScript. An optional BasePath is specified to locate assets
* relative to the base directory.
*/
try
{
// Render HTML to PDF, specifying the local path for asset resolution.
// The HTML content is rendered using the specified BasePath, which helps locate any local assets.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
// Save the resulting PDF to a file named "html-with-assets.pdf".
myAdvancedPdf.SaveAs("html-with-assets.pdf");
}
catch (Exception ex)
{
// Handle any errors that occur during rendering or saving.
// The error message is printed to the console for debugging and logging purposes.
Console.WriteLine("An error occurred: " + ex.Message);
}
Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
'
' * This code uses the IronPdf library to create a PDF from HTML content.
' * The ChromePdfRenderer provides a simple interface for rendering HTML and DOM as PDF.
'
' Advanced Example with HTML Assets
'
' * The following example demonstrates how to render a PDF from HTML containing external assets
' * such as images, CSS, and JavaScript. An optional BasePath is specified to locate assets
' * relative to the base directory.
'
Try
' Render HTML to PDF, specifying the local path for asset resolution.
' The HTML content is rendered using the specified BasePath, which helps locate any local assets.
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
' Save the resulting PDF to a file named "html-with-assets.pdf".
myAdvancedPdf.SaveAs("html-with-assets.pdf")
Catch ex As Exception
' Handle any errors that occur during rendering or saving.
' The error message is printed to the console for debugging and logging purposes.
Console.WriteLine("An error occurred: " & ex.Message)
End Try
This is the file that the code produced:
Frequently Asked Questions
What is IronPDF?
IronPDF is a C# library that allows developers to create PDF documents in C#, F#, and VB.NET for .NET Core and .NET Framework. It supports rendering HTML strings into PDFs using a fully functional version of the Google Chromium engine.
How do I convert an HTML string to a PDF using IronPDF?
To convert an HTML string to a PDF using IronPDF, you can use the ChromePdfRenderer class to render the HTML content and save it as a PDF.
How can I download IronPDF?
You can download the IronPDF C# library from NuGet. Visit the NuGet website and search for IronPDF to find the package.
What is the RenderHtmlAsPdf method?
The RenderHtmlAsPdf method is used to render an HTML string into a PDF document. It takes an HTML string as a parameter and returns a PdfDocument object, which contains the rendered PDF information.
How can I include external assets in my PDF?
You can include external assets in your PDF by setting the BaseUrlOrPath property of the ChromePdfRenderer.RenderingOptions. This property specifies the relative file path or URL context for hyperlinks, images, CSS, and JavaScript files.
Can I disable local disk access when rendering PDFs?
Yes, you can disable local disk access and cross-origin requests by setting the ChromePdfRenderer.EnableWebSecurity property to true. This ensures enhanced security when rendering HTML content from external sources.
What is the purpose of the BaseUrlOrPath property?
The BaseUrlOrPath property is used to define the base path or URL context for loading external resources such as images, CSS, and JavaScript files when rendering HTML to PDF.
How do I save the generated PDF after rendering?
After rendering the HTML string to a PDF using the ChromePdfRenderer, you can save the generated PDF by calling the SaveAs method on the PdfDocument object, specifying the desired file path.