Skip to content

Latest commit

 

History

History
317 lines (253 loc) · 13.4 KB

File metadata and controls

317 lines (253 loc) · 13.4 KB
title HTML to PDF conversion in .NET PDF Library | Syncfusion
description Discover how to convert HTML to PDF using the Blink rendering engine, with various features like TOC, partial web page to PDF, and more.
platform document-processing
control PDF
documentation UG

Converting HTML to PDF

The HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It uses popular rendering engines such as Blink (Google Chrome) and is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.

Syncfusion® HTML-to-PDF converter will work seamlessly in various platforms like Azure Cloud or web apps, Azure Functions, Amazon Web Service (AWS), Docker, WinForms, WPF, ASP.NET MVC, ASP.NET Core with Windows, Linux, and macOS.

Key features for HTML Converter

Install HTML to PDF .NET library to your project

Include the HTML to PDF converter in your project using two approaches.

  • NuGet packages (Recommended)
  • Assemblies.

NuGet Packages Required (Recommended)

Directly install the NuGet packages to your .NET application from nuget.org.

N> The HTML to PDF converter library internally uses the Blink rendering engine for the conversion. The binaries will differ for Windows, Linux, Mac, and AWS. So, separate packages are provided based on OS. Include the packages based on your requirement.

Platform(s) NuGet Package
(.NET Core, .NET 8, .NET 9) Windows {{'[Syncfusion.HtmlToPdfConverter.Net.Windows.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Windows/)'| markdownify }}
(.NET Core, .NET 8, .NET 9) Linux {{'[Syncfusion.HtmlToPdfConverter.Net.Linux.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Linux/)'| markdownify }}
(.NET Core, .NET 8, .NET 9) Mac {{'[Syncfusion.HtmlToPdfConverter.Net.Mac.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Mac/)'| markdownify }}
(.NET Core, .NET 8, .NET 9) AWS {{'[Syncfusion.HtmlToPdfConverter.Net.Aws.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Aws/)'| markdownify }}

Use the following packages for .NET Framework targeted applications. If you are using other Syncfusion libraries or components, use the HTML to PDF converter library with the same platform packages.

Platform(s) NuGet Package
Windows Forms {{'[Syncfusion.HtmlToPdfConverter.WinForms.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.WinForms/)'| markdownify }}
WPF {{'[Syncfusion.HtmlToPdfConverter.Wpf.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Wpf/)'| markdownify }}
ASP.NET {{'[Syncfusion.HtmlToPdfConverter.AspNet.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.AspNet/)'| markdownify }}
ASP.NET MVC {{'[Syncfusion.HtmlToPdfConverter.AspNet.Mvc5.nupkg](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.AspNet.Mvc5/)'| markdownify }}

Assemblies Required

Get the following required assemblies by downloading the HTML converter installer. Download and install the HTML converter for Windows, Linux, and Mac, respectively. Please refer to the advanced installation steps for more details.

Platforms Assemblies
WinForms WPF ASP.NET ASP.NET MVC
  • Syncfusion.Compression.Base.dll
  • Syncfusion.Pdf.Base.dll
  • Syncfusion.HtmlConverter.Base.dll
  • Newtonsoft.Json package (v13.0.1 or above)
.NET/.NET Core Blazor
  • Syncfusion.Compression.Portable.dll
  • Syncfusion.Pdf.Portable.dll
  • Syncfusion.HtmlConverter.Portable.dll
  • Newtonsoft.Json package (v13.0.1 or above)

Get Started with HTML to PDF conversion

Convert HTML to PDF in C#

Integrating HTML to PDF converter library in any .NET application is simple. Please refer to the following steps to include HTML to PDF conversion in your application.

Steps to convert HTML to PDF in .NET application

Step 1: Create a new .NET console application. Create .net core console sample

Select target .net core version

Step 2: Install Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your .NET application from NuGet.org. Install HTML to PDF converter .NET package

Step 3: Include the following namespace in your class file.

{% highlight c# tabtitle="C#" %}

using Syncfusion.Pdf; using Syncfusion.HtmlConverter;

{% endhighlight %}

Step 4: Use the following code sample to convert the URL to PDF in the program.cs.

{% tabs %}

{% highlight c# tabtitle="C#" %}

//Initialize HTML to PDF converter. HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); //Set Blink viewport size. blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0); //Assign Blink converter settings to HTML converter. htmlConverter.ConverterSettings = blinkConverterSettings; //Convert URL to PDF document. PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); //Create a filestream. FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite); //Save and close the PDF document. document.Save(fileStream); document.Close(true);

{% endhighlight %}

{% endtabs %}

By executing the program, you will get the PDF document as follows. htmltopdfoutput

A complete working sample can be downloaded from Github.

Convert HTML to PDF in Linux

HTML to PDF converter .NET library supports conversion in Linux. Refer to this section for more information about HTML to PDF conversion in Linux.

Convert HTML to PDF in Docker

HTML to PDF converter .NET library supports conversion in Docker. Refer to this section for more information about HTML to PDF conversion in Docker.

Convert HTML to PDF in Mac

HTML to PDF converter .NET library supports conversion in Mac. Refer to this section for more information about HTML to PDF conversion in Mac.

Convert HTML to PDF in ASP.NET Core

HTML to PDF converter .NET library supports conversion in ASP.NET Core. Refer to this section for more information about HTML to PDF conversion in ASP.NET Core.

Convert HTML to PDF in ASP.NET MVC

HTML to PDF converter .NET library supports conversion in ASP.NET MVC. Refer to this section for more information about HTML to PDF conversion in ASP.NET MVC.

Convert HTML to PDF in Blazor

HTML to PDF converter .NET library supports conversion in Blazor. Refer to this section for more information about HTML to PDF conversion in Blazor.

Convert HTML to PDF in Azure

HTML to PDF converter .NET library supports conversion in Azure. Refer to this section for more information about HTML to PDF conversion in Azure.

Convert HTML to PDF in AWS

HTML to PDF converter .NET library supports conversion in AWS. Refer to this section for more information about HTML to PDF conversion in AWS.

Features

Refer to this section for more information about features in HTML to PDF converter, you can get the details, code examples and demo from this section.

Troubleshooting and FAQ

Refer to this section for troubleshooting HTML to PDF conversion failures and frequently asked questions.

Steps to disable IE warning while performing HTML To PDF using the IE rendering engine

By default, the PDF document generated with the IE rendering engine comes with the following warning message.

IEWarning

Please refer to the below code snippet to use the DisableIEWarning API to remove the default IE warning from the PDF document.

{% tabs %}

{% highlight c# tabtitle="C#" %}

//Initialize the HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.IE); IEConverterSettings settings = new IEConverterSettings(); //Disable Default IE warning message. settings.DisableIEWarning = true;
//Assign IE settings to HTML converter htmlConverter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Save and close the PDF document document.Save("Output.pdf"); document.Close(true);

{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET" %} 'Initialize the HTML to PDF converter Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.IE) Dim settings As New IEConverterSettings() 'Disable Default IE Warning Message settings.DisableIEWarning = true 'Assign IE settings to HTML converter htmlConverter.ConverterSettings = settings 'Convert URL to PDF Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")

'Save and close the PDF document document.Save("Output.pdf") document.Close(True)

{% endhighlight %}

{% highlight c# tabtitle="ASP.NET Core" %} //Currently, IE rendering engine does not support conversion in .NET Core platform {% endhighlight %}

{% endtabs %}

N>Please try our Blink engine to improve the quality and accuracy of the HTML to PDF conversion.