Convert an Excel document to PDF on Linux

5 Nov 20254 minutes to read

Syncfusion® XlsIO is a .NET Core Excel library used to create, read, edit and convert Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can convert an Excel document to PDF on Linux.

Steps to convert an Excel document to PDF on Linux

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio

Step 2: Name the project.

Name the project

Step 3: Select the framework and click Create button.

Framework version

Step 4: Install the following Nuget packages in your application from NuGet.org by execute the following command.

dotnet add package Syncfusion.XlsIO.Net.Core -v 29.1.41 -s https://www.nuget.org/
dotnet add package SkiaSharp.NativeAssets.Linux -v 3.119.0 -s https://www.nuget.org/
dotnet add package HarfBuzzSharp.NativeAssets.Linux -v 8.3.1.1 -s https://www.nuget.org/

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your applications to use our components.

Step 5: Include the following Namespaces in the Program.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using Syncfusion.Pdf;

Step 6: Add the following code snippet in Program.cs file.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Load existing Excel file
	IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));

    //Convert to PDF
    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    #region Save
    //Saving the workbook
    pdfDocument.Save(Path.GetFullPath("Output/Sample.pdf"));
    #endregion
}

Step 1: Create a new C# .NET Core console application. Create .NET Core console project in Visual Studio Code

Step 2: Name the project and create the project.

Name the project

Alternatively, create a ASP.NET Core console application using the following command in the terminal(Ctrl+`).

dotnet new console -o Convert-Excel-to-PDF
cd Convert-Excel-to-PDF

Step 3: Install the following Nuget packages in your application from NuGet.org by execute the following command.

dotnet add package Syncfusion.XlsIO.Net.Core -v 29.1.41 -s https://www.nuget.org/
dotnet add package SkiaSharp.NativeAssets.Linux -v 3.119.0 -s https://www.nuget.org/
dotnet add package HarfBuzzSharp.NativeAssets.Linux -v 8.3.1.1 -s https://www.nuget.org/

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your applications to use our components.

Step 4: Include the following Namespaces in the Program.cs file.

using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using Syncfusion.Pdf;

Step 5: Add the following code snippet in Program.cs file.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    //Load existing Excel file
	IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));

    //Convert to PDF
    XlsIORenderer renderer = new XlsIORenderer();
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    #region Save
    //Saving the workbook
    pdfDocument.Save(Path.GetFullPath("Output/Sample.pdf"));
    #endregion
}

A complete working example of how to convert an Excel document to PDF on Linux is present on this GitHub page.

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

Output File

Click here to explore the rich set of Syncfusion® Excel library (XlsIO) features.

An online sample link to convert an Excel document to PDF in ASP.NET Core.