Examples

HTML to PDF using .NET

In the following examples, you convert HTML to PDF three different ways. The first example illustrates using DynamicPDF HTML Converter while the second example illustrates using DynamicPDF Core Suite. The third example illustrates converting HTML to PDF using DynamicPDF Converter.

Watch the Video

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

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

ConversionOptions for advanced HTML to PDF conversion

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

Getting Started

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.

NuGet Package ID: ceTe.DynamicPDF.HtmlConverter.NET

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

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

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

NuGet Package ID: ceTe.DynamicPDF.Converter.NET

GitHub Example Project

Clone or view the example project at GitHub.

Clone or View Example Project on GitHub

DynamicPDF Converter and Generator Information

More information on all three products is available here:

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!