DEV Community

IronSoftware
IronSoftware

Posted on

HTML to PDF in C# (Guide For .NET Beginners)

If you're new to C# and need to convert HTML to PDF, you're in the right place. I'll walk you through the entire process step-by-step, from installing the library to generating your first PDF.

No prior PDF generation experience required—just basic C# knowledge.

What Do I Need to Get Started?

You'll need:

  • Visual Studio (any recent version, including the free Community edition)
  • A C# project (Console App, ASP.NET, or any .NET project type)
  • IronPDF library (we'll install this via NuGet)

That's it. Let's start with the simplest possible example.

How Do I Install IronPDF in My Project?

Open your project in Visual Studio and install IronPDF via NuGet Package Manager:

Option 1: Package Manager Console

Install-Package IronPdf
Enter fullscreen mode Exit fullscreen mode

Option 2: NuGet Package Manager UI

  1. Right-click your project in Solution Explorer
  2. Select "Manage NuGet Packages"
  3. Search for "IronPdf"
  4. Click "Install"

Once installed, you're ready to write code.

How Do I Convert My First HTML String to PDF?

Let's start with the absolute simplest example—converting an HTML string to a PDF file:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new [ChromePdfRenderer](https://ironpdf.com/blog/videos/how-to-render-html-string-to-pdf-in-csharp-ironpdf/)();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, PDF World!</h1>");
pdf.SaveAs("my-first-pdf.pdf");
Enter fullscreen mode Exit fullscreen mode

Run this code, and you'll find my-first-pdf.pdf in your project's output directory (usually bin/Debug/net8.0/).

That's the core concept: create a renderer, render HTML, save the PDF.

What If I Have More Complex HTML?

You can pass any valid HTML string to RenderHtmlAsPdf(). Here's an example with CSS styling:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var html = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; }
        h1 { color: #2c3e50; }
        .info-box {
            background: #ecf0f1;
            padding: 20px;
            border-radius: 8px;
        }
    </style>
</head>
<body>
    <h1>My Styled PDF</h1>
    <div class='info-box'>
        <p>This HTML includes CSS styling.</p>
        <p>IronPDF renders it just like a browser would.</p>
    </div>
</body>
</html>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("styled-pdf.pdf");
Enter fullscreen mode Exit fullscreen mode

Notice the @"" syntax—that's a C# verbatim string, which makes it easy to write multi-line HTML without escaping quotes.

Can I Convert an HTML File to PDF?

Absolutely. If you have an existing HTML file, use RenderHtmlFileAsPdf():

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("template.html");
pdf.SaveAs("output.pdf");
Enter fullscreen mode Exit fullscreen mode

Make sure template.html exists in the same directory as your executable, or provide the full path.

How Do I Convert a Web Page (URL) to PDF?

Use RenderUrlAsPdf() to convert any live web page:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("webpage.pdf");
Enter fullscreen mode Exit fullscreen mode

IronPDF will fetch the page, render it with Chromium (the same engine as Google Chrome), and generate a PDF.

What Are the Three Main Rendering Methods?

Here's a quick reference for the three core methods you'll use most often:

Method Use Case Example
RenderHtmlAsPdf() Convert HTML string to PDF renderer.RenderHtmlAsPdf("<h1>Title</h1>")
RenderHtmlFileAsPdf() Convert HTML file to PDF renderer.RenderHtmlFileAsPdf("page.html")
RenderUrlAsPdf() Convert web page to PDF renderer.RenderUrlAsPdf("https://example.com")

How Do I Add Images to My PDF?

Images work just like they do in regular HTML. Use <img> tags:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var html = @"
<h1>PDF with Image</h1>
<img src='https://via.placeholder.com/300' alt='Placeholder' />
<p>Images load from URLs or local file paths.</p>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("pdf-with-image.pdf");
Enter fullscreen mode Exit fullscreen mode

You can use remote URLs (like in the example) or local file paths.

Can I Use Bootstrap or Other CSS Frameworks?

Yes! IronPDF uses Chromium rendering, so modern CSS frameworks like Bootstrap, Tailwind, and Flexbox all work correctly.

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var html = @"
<!DOCTYPE html>
<html>
<head>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body>
    <div class='container mt-5'>
        <div class='alert alert-primary' role='alert'>
            This is a Bootstrap-styled alert in a PDF!
        </div>
    </div>
</body>
</html>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("bootstrap-pdf.pdf");
Enter fullscreen mode Exit fullscreen mode

The Bootstrap CSS loads from the CDN and renders perfectly in the PDF.

How Do I Handle Multi-Page PDFs?

IronPDF automatically handles page breaks. If your HTML content is longer than one page, it will flow naturally across multiple pages:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var html = @"
<h1>Multi-Page Document</h1>
<p style='page-break-after: always;'>This is page 1.</p>
<h2>Page 2</h2>
<p>This content appears on the second page.</p>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("multi-page.pdf");
Enter fullscreen mode Exit fullscreen mode

Use the CSS property page-break-after: always; to force a page break at a specific point.

What If I Need to Set PDF Metadata?

You can set metadata like title, author, and subject:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Document with Metadata</h1>");

pdf.MetaData.Title = "My First PDF";
pdf.MetaData.Author = "Your Name";
pdf.MetaData.Subject = "Learning IronPDF";

pdf.SaveAs("pdf-with-metadata.pdf");
Enter fullscreen mode Exit fullscreen mode

Right-click the PDF in Windows Explorer, select "Properties," and you'll see your metadata under the "Details" tab.

How Do I Control Page Size and Orientation?

Use RenderingOptions to set page size (A4, Letter, etc.) and orientation (Portrait or Landscape):

using IronPdf;
using IronPdf.Rendering;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;

var pdf = renderer.RenderHtmlAsPdf("<h1>Landscape A4 PDF</h1>");
pdf.SaveAs("landscape.pdf");
Enter fullscreen mode Exit fullscreen mode

Common paper sizes: PdfPaperSize.A4, PdfPaperSize.Letter, PdfPaperSize.Legal.

Can I Set Custom Margins?

Yes, margins are controlled via RenderingOptions.MarginTop, MarginBottom, MarginLeft, and MarginRight:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;

var pdf = renderer.RenderHtmlAsPdf("<h1>PDF with Custom Margins</h1>");
pdf.SaveAs("custom-margins.pdf");
Enter fullscreen mode Exit fullscreen mode

Margins are measured in millimeters.

How Do I Handle JavaScript in My HTML?

IronPDF executes JavaScript before rendering. If your HTML relies on JavaScript to populate content, it will work:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var html = @"
<div id='content'></div>
<script>
    document.getElementById('content').innerHTML = '<h1>Generated by JavaScript</h1>';
</script>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("javascript-pdf.pdf");
Enter fullscreen mode Exit fullscreen mode

IronPDF waits for JavaScript execution to complete before generating the PDF.

What About Asynchronous PDF Generation?

If you're building an ASP.NET application, use the async version of rendering methods:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var renderer = new ChromePdfRenderer();
var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Async PDF</h1>");
await pdf.SaveAsAsync("async-output.pdf");
Enter fullscreen mode Exit fullscreen mode

This prevents blocking the thread during PDF generation.

How Do I Troubleshoot Common Errors?

Error: "File is being used by another process"
Make sure you're not trying to overwrite a PDF that's currently open in a PDF viewer. Close the file and try again.

Error: "Could not find file 'template.html'"
Use the full file path, or ensure the HTML file is in the correct directory:

var pdf = renderer.RenderHtmlFileAsPdf(@"C:\Projects\MyApp\template.html");
Enter fullscreen mode Exit fullscreen mode

Error: "License key required"
IronPDF requires a license for production use. You can start with a free trial for development. Visit the IronPDF website for licensing details.

What's the Difference Between IronPDF and Other Libraries?

I've used several PDF libraries in C#. Here's why I recommend IronPDF for beginners:

  • Simple API: Three main methods (RenderHtmlAsPdf, RenderHtmlFileAsPdf, RenderUrlAsPdf) cover 90% of use cases
  • Modern CSS support: Uses Chromium, so Bootstrap, Flexbox, and CSS Grid all work
  • No external dependencies: Pure .NET library, no external binaries to manage
  • Cross-platform: Works on Windows, Linux, macOS, and Docker

Other libraries like wkhtmltopdf require managing external executables, and they don't support modern CSS. iTextSharp is powerful but much more complex for basic HTML-to-PDF conversion.

Can I Try IronPDF for Free?

Yes. IronPDF offers a free trial with no credit card required. Perfect for learning and development.

For production use, you'll need a license. Pricing starts at $749 for a single developer. Check the IronPDF website for the latest pricing.

Where Do I Go from Here?

Now that you've mastered the basics, explore these advanced features:

  • Headers and footers: Add page numbers, logos, and dynamic content
  • PDF merging: Combine multiple PDFs into one
  • PDF security: Password-protect and encrypt PDFs
  • Digital signatures: Sign PDFs programmatically
  • Form filling: Populate PDF forms from C#

IronPDF's official documentation covers all these topics in detail.

Quick Reference: Complete Beginner Example

Here's a complete, runnable example you can copy into a new C# Console App:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

namespace HtmlToPdfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var html = @"
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #2980b9; }
    </style>
</head>
<body>
    <h1>My First PDF from C#</h1>
    <p>This PDF was generated using IronPDF.</p>
    <p>It supports HTML, CSS, and JavaScript.</p>
</body>
</html>";

            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf(html);
            pdf.SaveAs("beginner-example.pdf");

            Console.WriteLine("PDF created successfully!");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Run this code, and you'll have a PDF in seconds.


Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.

Top comments (0)