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
- DynamicPDF HtmlConverter for .NET,
- DynamicPDF Converter for .NET, and
- DynamicPDF Core Suite for .NET.
Be certain to select the example for the product you are interested in learning about.
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 (Converter
)
Sample Code - C#
Converter.Convert(new Uri("https://en.wikipedia.org"), "SimpleConversion.pdf");
Steps for Creating a PDF Document from HTML (ConversionOptions
)
- Create a new
ConversionOptions
instance and in the constructor pass thePageSize
,PageOrientation
, and font as constructor arguments. - Add an author, title, header, and footer to the
ConversionOptions
instance. - Call the
Converter
Convert
method and pass aUri
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
- Create a new
PageInfo
instance and specify page size and orientation in the constructor. - Create a new
Uri
instance by specifying the HTML file. - Create a
HtmlLayout
object and add theUri
andPageInfo
instances in the constructor. - Add a
Header
andFooter
to theHtmlLayout
object instance. - Create a new
Document
instance by calling the HtmlLayout instance'sLayout
method. - 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
- Create an
HtmlConversionOptions
object instance and in the constructor set the options tofalse
. - 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. - 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.
- C# - HtmlToPdf.cs
- VB.NET - HtmlToPdf.vb
DynamicPDF Converter for .NET
Clone or view the example project at GitHub. This example code is in the following files.
- C# - HtmlFileConversion.cs
- VB.NET - HtmlFileConversion.cs
DynamicPDF HTML Converter
Clone or view the example project at GitHub. This example on this page is in the following files.
- C# - WithConversionOptions.cs
- VB.NET - WithConversionOptions.vb
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.
DynamicPDF Core Suite is available on NuGet and in the ceTe.DynamicPDF.CoreSuite.NET
package.
DynamicPDF Converter is available on NuGet and in the ceTe.DynamicPDF.Converter.NET
package.
DynamicPDF Converter and Generator Information
More information on all three products is available here: