Converting HTML to PDF

24 Jan 20246 minutes to read

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.

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

NOTE

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 5, .NET 6) Windows

Syncfusion.HtmlToPdfConverter.Net.Windows.nupkg

(.NET Core, .NET 5, .NET 6) Linux

Syncfusion.HtmlToPdfConverter.Net.Linux.nupkg

(.NET Core, .NET 5, .NET 6) Mac

Syncfusion.HtmlToPdfConverter.Net.Mac.nupkg

(.NET Core, .NET 5, .NET 6) AWS

Syncfusion.HtmlToPdfConverter.Net.Aws.nupkg

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

WPF

Syncfusion.HtmlToPdfConverter.Wpf.nupkg

ASP.NET

Syncfusion.HtmlToPdfConverter.AspNet.nupkg

ASP.NET MVC

Syncfusion.HtmlToPdfConverter.AspNet.Mvc5.nupkg

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.

  • C#
  • using Syncfusion.Pdf;
    using Syncfusion.HtmlConverter;

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

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

    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.

    //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);
    '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)
    //Currently, IE rendering engine does not support conversion in .NET Core platform

    NOTE

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