HTML to PDF in C# (.NET & .NET Core)

Convert HTML to PDF in C#, .NET, .NET Core and Standard

HTML to PDF conversion can be done using the DynamicPDF Converter or DynamicPDF Generator product.

How to Convert HTML to PDF (.NET)

Below are steps and sample code to convert HTML files to PDF documents using the DynamicPDF Converter product. Basic HTML to PDF conversion can be done with one method call, however, if required advanced HtmlConversionOptions class can be used.

Steps for Creating a PDF Document from HTML

  1. Create a HtmlConversionOptions object to set the options.
  2. Create a HtmlConverter object by specifying the HTML file path as a URI and the options.
  3. Invoke the Convert method on the HtmlConverter and specify the PDF file name.

Sample code - C#

HtmlConversionOptions options = new HtmlConversionOptions(false);

HtmlConverter htmlConverter = new HtmlConverter(new Uri(@"http://www.google.com"), options);

htmlConverter.Convert("output.pdf");

How to Convert HTML to PDF using DynamicPDF Generator (.NET Core)

The HtmlLayout class can be used to convert HTML to PDF using Generator with several custom options for page info, header and footer for the output PDF.

Steps for Creating a PDF Document using Generator

  1. Create PageInfo with required page options.
  2. Create a URI object to point to the HTML file.
  3. Create a HtmlLayout object by specifying the URI and PageInfo.
  4. Create a Header and Footer as required to the HtmlLayout object.
  5. Convert the URI to Document by calling Layout method and save the Document.

Sample code - C#

PageInfo layoutPage = new PageInfo(PageSize.A4, PageOrientation.Portrait);
Uri uri = new Uri(@"http://www.google.com");

HtmlLayout html = new HtmlLayout(uri, layoutPage);

html.Header.Center.Text = "%%PR%%%%SP%% of %%ST%%";
html.Header.Center.HasPageNumbers = true;
html.Header.Center.Width = 200;

html.Footer.Center.Text = "%%PR%%%%SP(A)%% of %%ST(B)%%";
html.Footer.Center.HasPageNumbers = true;    
html.Footer.Center.Width = 200;

Document document = html.Layout();
document.Draw("output.pdf");

Getting Started

NuGet Package

DynamicPDF Generator is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The Core Suite package includes Generator, Merger and ReportWriter.

Nuget Logo
NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

Example Project

An example project showing this functionality can be cloned or viewed from GitHub:

GitHub Logo
Clone or View Example Project on GitHub

DynamicPDF Converter and Generator Information

More information can be found here:

Available in other platforms

DynamicPDF Generator is available for the following platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 17 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!