Convert HTML to PDF file in Linux

20 Jul 20264 minutes to read

The HTML to PDF converter is a .NET library that converts HTML or web pages to PDF documents in Linux.

Prerequisites

Version Compatibility

The Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package uses the Blink rendering engine for HTML to PDF conversion. This library is compatible with .NET 8.0 and later versions

Supported Inputs

The HTML to PDF converter supports the following input types:

  • HTML String: Direct HTML content.
  • URL: Web pages and online HTML content.
  • HTML Files: Local HTML files.
  • MHTML Files: Web archive (.mhtml/.mht) content.
  • Authenticated Web Pages: Pages that require cookies, form authentication, or HTTP authentication.
  • HTTP GET/POST Requests: HTML content accessed through GET or POST methods

Required Software

  • .NET 8.0 or later
  • Visual Studio 2022 or later
  • Linux OS (Ubuntu, CentOS, or other distributions)

Register the license key

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you must add the “Syncfusion.Licensing” assembly reference and register a license key in your application. Please refer to this link for details on registering a Syncfusion® license key.

Include a license key in your Program.cs file before creating an HtmlToPdfConverter instance. Refer to the Syncfusion License documentation to learn about registering the Syncfusion license key in your application.

using Syncfusion.Licensing;

public class Program
{
    // Register the Syncfusion license
    SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
}

NOTE

Starting from version 29.2.4, it is no longer necessary to manually add the following command-line arguments when using the Blink rendering engine:

settings.CommandLineArguments.Add("--no-sandbox");
settings.CommandLineArguments.Add("--disable-setuid-sandbox");

These arguments are only required when using older versions of the library that depend on Blink in sandbox-restricted environments.

Steps to convert HTML to PDF in .NET Core application on Linux

Step 1: Execute the following command in the Linux terminal to create a new .NET Core Console application.

dotnet new console

Convert HTMLToPDF Linux Step1

Step 2: Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your project from NuGet.org by executing the following command:

dotnet add package Syncfusion.HtmlToPdfConverter.Net.Linux -v xx.x.x.xx -s https://www.nuget.org/

Convert HTMLToPDF Linux Step2

Step 3: Add the following namespaces to your Program.cs file:

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

Step 4: Add code to the Program.cs file to convert HTML to PDF using the Convert method in the HtmlToPdfConverter class:

// Initialize HTML to PDF converter with Blink rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Create Blink converter settings for output rendering
BlinkConverterSettings settings = new BlinkConverterSettings();
// Assign Blink converter settings to HTML converter instance
htmlConverter.ConverterSettings = settings;
// Convert URL to PDF document using Blink rendering
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
// Create file stream to write PDF output to disk
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
// Save the PDF document to file stream
document.Save(fileStream);
// Close the document and dispose resources
document.Close(true);

Step 5: Execute the following command to restore the NuGet packages:

dotnet restore

Convert HTMLToPDF Linux Step3

Step 6: Execute the following command in the terminal to run the application:

dotnet run

Convert HTMLToPDF Linux Step4

By executing the program, the application will generate and save the PDF document in the same directory as the Program.cs file:
Convert HTMLToPDF Linux Step5

A complete working sample for converting HTML to PDF in Linux can be downloaded from GitHub.

Click here to explore the rich set of Syncfusion® HTML to PDF converter library features.

You can also view the online sample to convert HTML to PDF documents in ASP.NET Core.