HTML String to PDF
With IronPDF, you can create new PDF documents from simple HTML strings within your .NET project, and IronPDF is able to be used in C#, F#, and VB.NET. Thanks to the use of the ChromePdfRenderer
class, you can be sure that any PDF documents you render from HTML strings will come out pixel-perfect. With IronPDF's powerful HTML to PDF conversion features, you create high-quality PDF files tailored to fit your personal needs.
The 4 Steps to Converting HTML String to PDF
- Import the IronPDF library.
- Initialize a new
ChromePdfRenderer
object. - Use the
RenderHtmlAsPdf
method. - Save the PDF using
PdfDocument.SaveAs
.
See the code example below for more details:
// Import the IronPdf library, enabling use of its classes and methods for PDF generation.
using IronPdf;
// Step 2: Initialize a new ChromePdfRenderer object.
var renderer = new ChromePdfRenderer();
// Step 3: Render a simple HTML string into a PDF document.
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Advanced Step: Render HTML with assets like images.
// The optional BasePath parameter helps locate external assets.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
// Step 4: Save the basic PDF document to the file system.
pdf.SaveAs("output.pdf");
// Import the IronPdf library, enabling use of its classes and methods for PDF generation.
using IronPdf;
// Step 2: Initialize a new ChromePdfRenderer object.
var renderer = new ChromePdfRenderer();
// Step 3: Render a simple HTML string into a PDF document.
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Advanced Step: Render HTML with assets like images.
// The optional BasePath parameter helps locate external assets.
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
// Step 4: Save the basic PDF document to the file system.
pdf.SaveAs("output.pdf");
The first step to converting an HTML string to a PDF in C# is ensuring that you have the IronPDF library properly set up and working within your project. By including using IronPdf
, we make sure we can access the classes needed from the IronPDF library to carry out HTML to PDF conversion. The next line, Installation.EnableWebSecurity = true
, is conceptually used to disable local disk access or cross-origin requests, ensuring secure operations. (Note: This line was missing from the example but usually pertains to configuration settings to secure PDF rendering operations.)
The example demonstrates how to create an instance of ChromePdfRenderer
which handles the conversion of HTML to PDF. The RenderHtmlAsPdf
method is used to convert a simple HTML string ("<h1>Hello World</h1>"
) into a PDF document. This document is saved to the disk using the SaveAs
method.
In the advanced example, IronPDF is shown to handle HTML content containing external assets such as images, CSS, and JavaScript. To load these assets, the optional BasePath
parameter is used, specifying the directory containing the required files. The resulting PDF, which includes the external assets, is saved using the same SaveAs
method. This code example highlights IronPDF's ability to handle both basic and complex HTML content, making it an efficient tool for generating PDFs programmatically.
For more examples, check the How-to Guide on using IronPDF with C#.