Free Support Forum -

Excel to PDF Conversion using C# in a Docker Container for Ubuntu/Linux


Docker has become standard for millions of people in the software industry to build and develop software in packages called containers. Containers are isolated from each other and contain all the necessary packages, library files, configuration files. It separates the application from the infrastructure as everything required to run the application is bundled into it.

You can use Aspose.Cells in containers to process spreadsheets. In this article, we will demonstrate the Excel to PDF conversion and guide you to create your own container having all the necessary components to run this application.

Environment Setting

It is assumed that you have installed docker on your computer and then pulled the microsoft/dotnet container as follows:

docker pull microsoft/dotnet

Once you have pulled it, run the container and execute the following commands in the terminal.

Step 1:
apt update && apt upgrade

Step 2:
apt install wget

Step 3:
apt-get install sudo

Step 4:
wget -O packages-microsoft-prod.deb

Step 5:
sudo dpkg -i packages-microsoft-prod.deb

Step 6:

sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-3.1

Step 7:

sudo apt-get install libgdiplus
ln -s gdiplus.dll

Step 8:

sudo apt-get install libc6-devStep 1:
apt update && apt upgrade

Once you have installed all the necessary libraries, you can develop your simple .NET Core based console application in Visual Studio 2019 to convert Excel to PDF and save it on your host computer containing the container configured above.

Sample Code

As we are demonstrating the Excel to PDF conversion, your console application may look like as follows:

// Instantiate new workbook
Workbook workbook = new Workbook("sample.xlsx");

// Define PdfSaveOptions
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

// Set the compliance type
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;

// Save the file
workbook.Save("output.pdf", pdfSaveOptions);  

In this sample code, we have used Workbook to load the existing Excel file. Then pdf save option for compliance is set using the PdfSaceOptions. Finally, we save the output PDF file.

Once this application is compiled, now we will link our host system folder with the folder in the container before running the container as follows:

docker run -it -v ~/Desktop:/Desktop Microsoft/dotnet /bin/bash

We have linked our desktop folder with a folder named desktop in the container and now we can navigate to our compiled application folder having .proj file.

Run the following command and you will get the application executed.

dotnet run


After running this command you will get PDF in the same folder.
Source Excel File:

Output PDF:

For more information on Excel to PDF conversion, have a look at the following article:
Excel To PDF Conversion