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

Download the NuGet package and Github project to get started. The following steps illustrates how you can easily convert HTML to PDF using a single line of code when using the DynamicPDF HTML Converter.

Steps for Creating a PDF Document from HTML

  1. Call the Converter Convert method by passing a new Uri that needs to be converted and the output PDF file name.

Sample code - C#

Converter.Convert(new Uri("https://www.google.com"), "SimpleConversion.pdf");

ConversionOptions for advanced HTML to PDF conversion

  1. Create a new ConversionOptions that passes the PageSize, PageOrientation, and font as constructor arguments.
  2. Add an author, title, header, and footer to the ConversionOptions object.
  3. Call the Converter Convert method by passing a new Uri that needs to be converted and the output PDF file name.

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://www.google.com"), "WithConversionOptions.pdf", options);

NuGet Package

Download DynamicPDF HTML Converter NuGet ceTe.DynamicPDF.HtmlConverter.NET package.

NuGet Package ID: ceTe.DynamicPDF.HtmlConverter.NET

Example Github Project

Clone or view the example GitHub project:

Clone or View Example Project on GitHub

How to Convert HTML to PDF using Converter for .NET

Above you used the HTML Converter product which is specific to converting HTML to PDF. In the following example, you use the Converter product that can handle over 50 file types including MS Office.

you can use the HtmlConversionOptions and HTMLConverter classes to convert HTML text or a file to PDF.

Steps for Creating a PDF Document from HTML

  1. Create a HtmlConversionOptions object and set the options to false.
  2. Create a HtmlConverter object by specifying the new URI object.
  3. Invoke the HtmlConverter Convert method 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);


How to Convert HTML to PDF using the DynamicPDF Generator

Use the HtmlLayout class to convert HTML to PDF using DynamicPDF Generator. In the following example we illustrate using the DynamicPDF Generator while specifying several custom page info, header and footer options for the outputed PDF.

Steps for Creating PDF out of HTML using Generator

  1. Create PageInfo with required page options.
  2. Create a Uri object by specifying the HTML file.
  3. Create a HtmlLayout object by adding the Uri and PageInfo.
  4. Add a Header and Footer to the HtmlLayout object.
  5. Convert the Uri to Document by calling Layout method and save the Document using the Draw method.

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

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 Package ID: ceTe.DynamicPDF.CoreSuite.NET

GitHub Example Project

Clone or view the example project at GitHub. This example is is the Examples/HtmlToPDF.cs file.

Clone or View Example Project on GitHub

DynamicPDF Converter and Generator Information

More information is available here:

Java and COM/ActiveX Platforms

The DynamicPDF Generator is also available for Java and COM/ActiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

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

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