We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET Core |
Control: PDF |
Published Date: February 23, 2018 |
Last Revised Date: April 26, 2019

Syncfusion HTML to PDF for .NET used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can convert HTML to PDF in ASP.NET Core application using .Net Standard 2.0.

Minimum product version: 16.1.0.24

Minimum .NET core version: 2.0.0

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:

$ sudo apt-get update

$ sudo apt-get install xvfb

$ sudo apt-get install libssl-dev

$ sudo apt-get install libx11-dev libx11-xcb-dev libxcb-icccm4-dev libxcb-image0-dev libxcb-keysyms1-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-render0-dev libxcb-shm0-dev libxcb-util0-dev libxcb-xfixes0-dev libxcb-xkb-dev libxcb1-dev libxfixes-dev libxrandr-dev libxrender-dev

The following assemblies or NuGet package is required for converting HTML to PDF in ASP.NET Core Linux.

Assemblies required:

  • Syncfusion.Compression.Portable.dll
  • Syncfusion.Pdf.Portable.dll
  • Syncfusion.HtmlConverter.Portable.dll
  • QtBinaries

You can get all the previously mentioned assemblies, QtBinaries, and working samples by installing the HTML converter installer for Linux available in the following link.

HTML converter Linux (zip file): https://www.syncfusion.com/downloads/latest-version

HTML converter Linux

NuGet

Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org. The NuGet package contains QtBinaries for all platforms (Windows, Linux and Mac). Copy the QtBinariesLinux folder from the NuGet package installed location and paste it into your application folder to use it for conversion.

NuGet Installed location: /.nuget/packages/syncfusion.htmltopdfconverter.qtwebkit.net.core/XX.X.X.XX/lib

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

  1. Extract the zip file (syncfusionessentialwebkit.zip) in Linux and open the QtBinaries folder in file explorer.
  2. Provide the execute permission for Syncfusion.WebKitWrapper file inside the QtBinaries folder. Refer to the following screenshot. Allow permission
  3. Open the project folder (HtmlToPdf.csproj) in Linux terminal, then, enter the following commands one by one to run the sample.
  • dotnet restore
  • dotnet build
  • dotnet run
  1. Enter the localhost server path in the web browser to view the result. Localhost server path

Use the following code snippet to convert HTML to PDF in ASP.NET Core Linux.

C#

//To get content root path of the project
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
   _hostingEnvironment = hostingEnvironment;
}
 
public IActionResult ExportToPDF()
{
 //Initialize HTML to PDF converter 
 HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
 WebKitConverterSettings settings = new WebKitConverterSettings();
  //Set WebKit path
  settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesLinux");
  //Assign WebKit settings to HTML converter
  htmlConverter.ConverterSettings = settings;
  //Convert URL to PDF
  PdfDocument document = htmlConverter.Convert("https://www.google.com");
  MemoryStream stream = new MemoryStream();
  document.Save(stream);
  return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf");
}

 

A complete working sample can be downloaded from HTMLtoPDF_Linux.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.

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon