Articles in this section
Category / Section

How to convert HTML to PDF in ASP.NET Core Linux?

2 mins read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage. Using this library, you can convert an HTML to PDF in a Linux.

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

1. For converting HTML to PDF in Linux, the following pre-requisite packages should be installed in the Linux machine. To do that, enter the following commands in the terminal:

C#:

$ sudo apt-get update
$ sudo apt-get install -yq --no-install-recommends \ 
     libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \ 
     libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \ 
     libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \ 
     libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \ 
     libnss3 libgbm1

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

C#:

dotnet new console

 

Create new console application in Linux terminal.

3. Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your project from NuGet.org by execute the following command. 

C#:

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

     Install the latest Blink Linux NuGet package

 

4. Include the following namespaces and code snippet in Program.cs file

C#:

// [C# Code]
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;

 

C#:

// [C# Code]
//Initialize HTML to PDF converter 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
 
BlinkConverterSettings settings = new BlinkConverterSettings();
     
//Set command line arguments to run without sandbox. settings.CommandLineArguments.Add("--no-sandbox");
 
settings.CommandLineArguments.Add("--disable-setuid-sandbox");
     
//Assign Blink settings to HTML converter
htmlConverter.ConverterSettings = settings;
 
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
 
FileStream fileStream = new FileStream("Sample.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
 
//Save and close the PDF document 
document.Save(fileStream);
 
document.Close(true);

 

5.Execute the following command to restore the NuGet packages.

C#:

dotnet restore

 

Restore the project.

 

6. Execute the following command in terminal to run the application.

 

C#:

dotnet run

 

Run the project.

 

By executing the program, you will get the PDF document as follows. The output will be saved in parallel to program.cs file.

Output document.

You can download a complete working sample from Linux-HTML-to-PDF-Demo.zip.

Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and hyperlinks.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied