HTML to PDF using .NET
In the following examples, you convert HTML to PDF three different ways. You,
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
- Call the
Converter
Convert
method by passing a newUri
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
- Create a new
ConversionOptions
that passes thePageSize
,PageOrientation
, and font as constructor arguments. - Add an author, title, header, and footer to the
ConversionOptions
object. - Call the
Converter
Convert
method by passing a newUri
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.
Example Github Project
Clone or view the example GitHub project:
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
- Create a
HtmlConversionOptions
object and set the options tofalse
. - Create a
HtmlConverter
object by specifying the new URI object. - 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
- Create
PageInfo
with required page options. - Create a
Uri
object by specifying the HTML file. - Create a
HtmlLayout
object by adding theUri
andPageInfo
. - Add a
Header
andFooter
to theHtmlLayout
object. - Convert the
Uri
toDocument
by callingLayout
method and save theDocument
using theDraw
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.
GitHub Example Project
Clone or view the example project at GitHub. This example is contained in the Examples/HtmlToPDF.cs file.
DynamicPDF Converter and Core Suite Information
More information is available here:
- The DynamicPDF HTML Converter for .NET
- The DynamicPDF Converter for .NET
- The DynamicPDF Core Suite for .NET
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.
- Java - DynamicPDF Generator for Java
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX