IRONSOFTWAREHOME
HTML String to PDF
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    // Create a PDF from an HTML string
    const pdf = await PdfDocument.fromHtml("<h1>Hello World</h1>");

    // Export the PDF to a file
    await pdf.saveAs("output.pdf");

    // Advanced Example with HTML Assets
    // Load external HTML assets: Images, CSS, and JavaScript.
    const htmlContentWithAssets = "<img src='icons/iron.png'>";
    const advancedPdf = await PdfDocument.fromHtml(htmlContentWithAssets);

    // Save the PDF with loaded assets
    await advancedPdf.saveAs("html-with-assets.pdf");
})();
NuGet Download
Install-Package @ironsoftware/ironpdf
HTML String to PDF

HTML String to PDF

Use the fromHtml method to convert HTML strings and files into PDF documents. The resulting PDF document should be identical to the one generated by the print feature of the Google Chrome browser. IronPDF utilizes the Chrome engine for rendering HTML to PDF documents, ensuring the same output when using the same configuration as Google Chrome.

In addition to its rendering capabilities, IronPDF offers a wealth of features for manipulating PDF documents. It seamlessly combines rendering and manipulation into a set of new features. For example, consider the HTML stamping feature of IronPDF, which involves rendering HTML and merging the resulting PDF with the main HTML content to produce a final document with HTML stamps either behind or in front of the primary content.

Explanation of the C# Code

  1. Library Import: The IronPdf namespace is imported to allow the use of IronPDF library functions for HTML to PDF conversion.
  2. HTML Definition: A simple HTML string is defined, which will be rendered into a PDF.
  3. HtmlToPdf Renderer Initialization: An instance of HtmlToPdf is created. This instance will be responsible for handling the conversion of HTML to PDF.
  4. Rendering HTML to PDF: The RenderHtmlAsPdf method on the HtmlToPdf object is called with the HTML string. This method converts the HTML content into a PDF document.
  5. Saving the PDF: The generated PDF document is saved to a file named output.pdf using the SaveAs method.
  6. User Notification: The console outputs a message notifying the user that the PDF has been generated and saved successfully.
Learn to Convert HTML to PDF in Node.js
View on GitHub

Ready to Get Started?

Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge related to IronPDF Product Demo

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required