How to Convert HTML to PDF in .NET Core
Mehr Muhammad Hamza

Mehr Muhammad Hamza @mhamzap10

About: Software Engineer | .Net Core | EF Core | Web API | SQL Server

Joined:
Jul 11, 2021

How to Convert HTML to PDF in .NET Core

Publish Date: Apr 20
0 0

In modern software development, especially in .NET Core applications, converting HTML to PDF is a common requirement. Whether you're generating invoices, exportable reports, or downloadable documents from HTML pages, having a reliable and developer-friendly solution is key.

IronPDF is a powerful .NET library that simplifies the task of converting HTML content into fully-rendered, styled, and printable PDF documents. It supports modern HTML5, CSS, and JavaScript, making it capable of handling complex layouts, responsive designs, and embedded resources with ease.

In this article, we’ll walk through how to use IronPDF in a .NET Core console application to convert HTML content into PDF documents. Each step is explained with context, and we’ll cover the most common use cases without overwhelming you with edge-case complexities.

Getting Started with IronPDF

Before we write any code, let’s first install IronPDF and prepare our project.

  1. Create a new console project
  2. Install the IronPDF NuGet package:
Install-Package ironPDF
Enter fullscreen mode Exit fullscreen mode

This will add the IronPDF library to your project, allowing you to start working with PDF generation right away.

Example # 1: Convert a Simple HTML String to PDF

Let’s start with the most basic case—converting a small, static HTML snippet into a PDF file.

What we’re going to do:

We’ll use IronPDF to render a simple string of HTML that contains a header and a paragraph. Then we’ll save it as a PDF file named simple.pdf.

using IronPdf;

var renderer = new ChromePdfRenderer();

string htmlContent = "<h1>Welcome to IronPDF</h1><p>This PDF was generated from a basic HTML string.</p>";

var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("simple.pdf");
Enter fullscreen mode Exit fullscreen mode

What this code does:

  • We create an instance of ChromePdfRenderer, which uses a headless Chrome engine under the hood.
  • A basic HTML string is passed to the RenderHtmlAsPdf() method.
  • The resulting PDF is saved to the disk as simple.pdf.

This is the simplest and fastest way to turn HTML into a PDF file using IronPDF.

Create PDF from HTML String

Example # 2: Convert HTML from an External File

What we’re going to do:

Sometimes, you already have an HTML file designed and ready to go. In this example, we’ll convert an external .html file into a PDF file named fromFile.pdf.

using IronPdf;


var renderer = new ChromePdfRenderer();
//  convert html files
var pdf = renderer.RenderHtmlFileAsPdf(@"D:\Tutorial Project\Website\index.html"); 
pdf.SaveAs("fromFile.pdf");
Enter fullscreen mode Exit fullscreen mode

What this code does:

  • IronPDF reads the HTML file template.html from the file system.
  • It parses and renders the file exactly as a browser would.
  • The PDF output is written to disk as fromFile.pdf.

This approach is ideal when working with pre-built templates for invoices, reports, or any standard document layout.

PDF from HTML File

Example # 3: Convert a Live Web Page (Public URL)

What we’re going to do:
If you need to convert a live webpage into a PDF—for example, a receipt page or documentation—we can pass the URL directly to IronPDF.

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf(@"https://www.wikipedia.org/");
pdf.SaveAs("webpage.pdf");
Enter fullscreen mode Exit fullscreen mode

What this code does:

  • The RenderUrlAsPdf() method fetches the live HTML from the specified URL.
  • The content is rendered into a PDF, preserving layout, styles, and images.
  • This PDF is saved as webpage.pdf.

It’s a quick and practical way to create downloadable versions of existing web pages.

HTML to PDF in .NET Core

Example # 4: Adding Headers and Footers to the PDF

What we’re going to do:
Now let’s enhance the PDF by adding a header and footer that will appear on every page. This is commonly used for page numbers, titles, or timestamps.

using IronPdf;


var renderer = new ChromePdfRenderer();

renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    MaxHeight = 15, 
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    MaxHeight = 10, 
    HtmlFragment = "<center><i>This is PDF Header<i></center>",
    DrawDividerLine = true
};

renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

var pdf = renderer.RenderUrlAsPdf(@"https://www.wikipedia.org/");
pdf.SaveAs("webpage.pdf");
Enter fullscreen mode Exit fullscreen mode

What this code does:

  • We set HtmlHeader and HtmlFooter using HtmlHeaderFooter for full HTML customization.
  • The header displays static HTML text; the footer uses placeholders like {page} and {total-pages} for page numbers.
  • Divider lines are added for a clean look, and margins are set to ensure proper spacing.
  • The rest of the webpage is rendered as usual.

This feature is great for web captures, reports, and polished, paginated PDFs.

Add Header and Footer in PDF .NET core

Example # 5: Styling the PDF with CSS

What we’re going to do:

Let’s add a bit of visual design to our PDF using embedded CSS. You can include styling directly in the HTML or reference external stylesheets.

using IronPdf;  

          var renderer = new ChromePdfRenderer();

            string html = @"
<html>
<head>
  <style>
    body { font-family: Arial; margin: 40px; }
    h1 { color: darkblue; }
    p { font-size: 14px; line-height: 1.6; }
  </style>
</head>
<body>
  <h1>Styled PDF</h1>
  <p>This PDF uses CSS for font styles, colors, and spacing.</p>
</body>
</html>";

            var pdf = renderer.RenderHtmlAsPdf(html);
            pdf.SaveAs("styledPdf.pdf");
Enter fullscreen mode Exit fullscreen mode

What this code does:

  • CSS styles are added directly inside the <style> tag in the <head>.
  • These styles control the appearance of text, spacing, and layout.
  • The resulting PDF preserves the exact visual formatting as intended.

This method is particularly useful when you want to apply brand colors, fonts, or responsive layouts in your documents.

Style PDF in .NET Core

When working with modern applications, especially those built using .NET Core, developers often need a reliable solution for converting .net core html to pdf. A robust pdf library not only acts as an efficient HTML to pdf converter but also provides features to split pdf documents, manage pdf document pages, and handle various customization options like web fonts. Whether you're dealing with single-page exports or need to manipulate multiple pdf pages, having a capable pdf file converter makes all the difference in ensuring high-quality and accurate outputs.

Conclusion

IronPDF offers a clean and developer-friendly API for converting HTML to PDF in .NET Core applications. Whether you're working with strings, files, or URLs, the process is simple and efficient. With just a few lines of code, you can:

  • Generate printable reports
  • Convert styled web content into PDFs
  • Add headers, footers, and page numbers
  • Apply custom CSS for a polished look

For most day-to-day needs, IronPDF strikes a good balance between simplicity and control. It's a solid choice when your application needs dynamic PDF generation without diving into complex rendering engines or markup conversion logic. You can get started quickly with a full-featured free trial to explore everything the library has to offer.

Comments 0 total

    Add comment