Examples

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

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. The easiest way to install the package is through the Visual Studio Package Manger. You can also download directly from NuGet.

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 which can handle over 50 file types, including MS Office. You can also use the HtmlConversionOptions and HTMLConverter classes to convert HTML text or convert a file to a 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);

htmlConverter.Convert("output.pdf");

How to Convert HTML to PDF using DynamicPDF Core Suite

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

Steps for Creating a PDF From HTML

  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();
document.Draw("output.pdf");

NuGet Package

DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The easiest way to install the package is through the Visual Studio Package Manager. You can also download the package directly from NuGet.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

GitHub Example Project

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

Clone or View Example Project on GitHub

DynamicPDF Converter and Core Suite Information

More information is available here:

Available on Other Platforms

The DynamicPDF Core Suite 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!