Skip to main content

HTML to PDF

Version: 3.0

Convert HTML to PDF

DynamicPDF makes converting HTML to PDF a breeze. Use DynamicPDF Converter, DynamicPDF HTML Converter, or DynamicPDF Core Suite to easily convert HTML to PDF. The following C# examples show you how.


Multiple DynamicPDF products support HTML conversion. Here we provide examples using each product. Examples for converting HTML to PDF are provided for

Be certain to select the example for the product you are interested in learning about.


Watch the Video​

YouTube thumbnail: Watch video: Convert HTML to PDFWatch video: Convert HTML to PDF

How to Convert HTML to PDF using HTML Converter for .NET​

DynamicPDF HTML Converter easily converts HTML to PDF. The following two examples illustrate converting using only one line of code followed by an example that adds conversion options to format the generated PDF.

Steps for Creating a PDF Document from HTML (Converter)​

  1. Call the Converter Convert method and pass a new URI and path to the PDF document to output.

Sample Code - C#​

Converter.Convert(new Uri("https://en.wikipedia.org"), "SimpleConversion.pdf");

Steps for Creating a PDF Document from HTML (ConversionOptions)​

  1. Create a new ConversionOptions instance and in the constructor pass the PageSize, PageOrientation, and font as constructor arguments.
  2. Add an author, title, header, and footer to the ConversionOptions instance.
  3. Call the Converter Convert method and pass a Uri to the HTML document to convert and the path to the PDF file to convert to.

Sample code - C#​

ConversionOptions options = new ConversionOptions(PageSize.A4, PageOrientation.Portrait, 50.0f);
// Set Metadata for the PDF
options.Author = "Myself";
options.Title = "My Webpage";
// Set Header and Footer text
options.Header = "<div style=\"text-align:center;display:inline-block;width:100%;font-size:12px;\">" +
"<span class=\"date\"></span></div>";
options.Footer = "<div style=\"text-align:center;display:inline-block;width:100%;font-size:12px;\">" +
"Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>";
// Convert with Options
Converter.Convert(new Uri("https://en.wikipedia.org"), "WithConversionOptions.pdf", options);

How to Convert HTML to PDF using the DynamicPDF Core Suite​

You can also use DynamicPDF Core Suite to generate a PDF document from HTML. Use the HtmlLayout class to convert HTML to PDF using DynamicPDF Core Suite. In the following example, we illustrate using DynamicPDF Core Suite and specify several custom formatting options such as: page info, header, and footer options for the generated PDF.

Steps for Creating PDF out of HTML using Core Suite​

  1. Create a new PageInfo instance and specify page size and orientation in the constructor.
  2. Create a new Uri instance by specifying the HTML file.
  3. Create a HtmlLayout object and add the Uri and PageInfo instances in the constructor.
  4. Add a Header and Footer to the HtmlLayout object instance.
  5. Create a new Document instance by calling the HtmlLayout instance's Layout method.
  6. Call the Document instance's Draw method and in the method call, pass the path to the PDF to create.

Sample code - C#​

PageInfo layoutPage = new PageInfo(PageSize.A4, PageOrientation.Portrait);
Uri uri = new Uri(@"http://en.wikipedia.org");

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");

How to Convert HTML to PDF using Converter for .NET​

In this final example, you use the DynamicPDF Converter product to convert HTML to PDF. DynamicPDF Converter handles over 50 file types including MS Office, converting HTML is just one of the many conversion options offered by DynamicPDF Converter.

Steps for Creating a PDF Document from HTML​

  1. Create an HtmlConversionOptions object instance and in the constructor set the options to false.
  2. Create a new HtmlConverter instance and in the constructor specify a new URI object to the HTML page to convert. Also pass the HtmlConversionOptions instance.
  3. Invoke the HtmlConverter Convert method and specify the PDF file name in the method call.

Sample code - C#​

HtmlConversionOptions options = new HtmlConversionOptions(false);

HtmlConverter htmlConverter = new HtmlConverter(new Uri(@"http://en.wikipedia.org"), options);

htmlConverter.Convert("output.pdf");

GitHub Project​

The following three projects contain examples for converting HTML to a PDF.

DynamicPDF Core Suite for .NET​

Clone or view the example project at GitHub. This example code is contained in the following files.

View source on GitHub: Core Suite .NET Core Examples

DynamicPDF Converter for .NET​

Clone or view the example project at GitHub. This example code is in the following files.

View source on GitHub: DynamicPDF Converter product examples

DynamicPDF HTML Converter​

Clone or view the example project at GitHub. This example on this page is in the following files.

View source on GitHub: DynamicPDF HTML Converter product examples

Getting Started​

Get started easily by installing DynamicPDF Core Suite for .NET through NuGet or manually. Then, refer to the documentation for more information on using and purchasing.

NuGet Package​

DynamicPDF HTML Converter, DynamicPDF Core Suite, and DynamicPDF Converter are available via NuGet. The easiest way to install the packages is through the Visual Studio Package Manager. DynamicPDF HTML Converter is available on NuGet in the ceTe.DynamicPDF.HtmlConverter.NET package.

Get ceTe.DynamicPDF.HtmlConverter.NET from NuGet DynamicPDF Core Suite is available on NuGet and in the ceTe.DynamicPDF.CoreSuite.NET package.

Get ceTe.DynamicPDF.CoreSuite.NET from NuGet DynamicPDF Converter is available on NuGet and in the ceTe.DynamicPDF.Converter.NET package.

Get ceTe.DynamicPDF.Converter.NET from NuGet

DynamicPDF Converter and Generator Information​

More information on all three products is available here: