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 4 Steps to Converting HTML String to PDF
- Import the IronPDF library.
- Initialize a new
ChromePdfRendererobject. - Use the
RenderHtmlAsPdfmethod. - Save the PDF using
PdfDocument.SaveAs.
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.
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.