Convert an Excel document to PDF in Linux Docker

22 Jul 20267 minutes to read

Docker is an open platform for developing, shipping, and running applications. You can use Essential® XlsIO in a Docker container to create, read, edit, and convert Microsoft Excel documents to various formats. On this page, you will learn how to convert an Excel document to PDF in a Linux Docker container using the Syncfusion® XlsIO library (Essential® XlsIO).

Steps to convert an Excel document to PDF in Linux Docker

Step 1: Create a new Core Console application.

Create a Console Application in Visual Studio

Step 2: Name the project.

Name the project

Step 3: Install the following NuGet packages as references to your project from NuGet.org.

Install Syncfusion.XlsIORenderer.Net.Core NuGet Package
Install SkiaSharp.NativeAssets.Linux v2.80.2 NuGet Package

NOTE

  1. If you’re deploying the application in a Linux environment, refer to the documentation for the required additional NuGet packages.

NOTE

  1. Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or from the NuGet feed, you must also add the Syncfusion.Licensing reference and register a license key. Refer to this link to learn how to register the Syncfusion® license key. The simplest approach is to add the following call at the top of Program.cs before constructing the ExcelEngine:
    Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR_LICENSE_KEY");

Step 4: Add the following namespaces in Program.cs.

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

Step 5: Add the following code in Program.cs. Place InputTemplate.xlsx in the project’s bin\Debug\net8.0 folder (or set its Copy to Output Directory property to Copy if newer) so the relative path resolves at runtime. Inside the container the working directory is /app, so the file is included via the Dockerfile step shown later.

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

    //Open the existing Excel workbook.
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");

    //Initialize the XlsIO renderer.
    XlsIORenderer renderer = new XlsIORenderer();

    //Convert the Excel document to a PDF document.
    PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

    //Save the converted PDF document.
    pdfDocument.Save("Output.pdf");
}

Step 6: Add Docker support to the application by right-clicking the project and choosing Add → Docker Support. Make sure Docker Desktop is installed and configured to run Linux containers (Windows defaults to Windows containers).

Add Docker support to that console app

Step 7: Choose Linux so the application runs in a Linux Docker container.

Choose Linux option

Step 8: Open the Dockerfile to view the default Docker commands shown below. This default Dockerfile installs font config and a basic TTF font so that PDF text renders correctly. It also copies InputTemplate.xlsx into the image.

FROM mcr.microsoft.com/dotnet/runtime:3.1 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Convert-Excel-to-PDF.csproj", "."]
RUN dotnet restore "./Convert-Excel-to-PDF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Convert-Excel-to-PDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Convert-Excel-to-PDF.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Convert-Excel-to-PDF.dll"]

Step 9: Set the run target to Docker in the Visual Studio toolbar and run the application.

Choose Docker and run the application

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

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

Output File

Dockerfile Examples

The following examples demonstrate how the Dockerfile should be configured to convert an Excel document to PDF in different Linux distributions. All examples target .NET 8 and install font config plus a basic TTF font so that PDF text renders correctly. If your workbook contains CJK, Arabic, or other non-Latin text, install the appropriate fonts-noto-* package as well.

Alpine

Use the following Dockerfile to convert an Excel document to PDF in Alpine Linux.

FROM mcr.microsoft.com/dotnet/aspnet:3.1-alpine3.12 AS base
RUN apk update && apk upgrade && apk add fontconfig
RUN apk add --update ttf-dejavu fontconfig
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:3.1-alpine3.12 AS build
WORKDIR /src
COPY ["Convert-Excel-to-PDF.csproj", "."]
RUN dotnet restore "./Convert-Excel-to-PDF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Convert-Excel-to-PDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Convert-Excel-to-PDF.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Convert-Excel-to-PDF.dll"]

A complete working example of converting an Excel document to PDF in Alpine Linux Docker container is present on this GitHub page.

Debian

Use the following Dockerfile to convert an Excel document to PDF in Debian Linux.

FROM mcr.microsoft.com/dotnet/aspnet:3.1-buster-slim AS base
RUN apt-get update -y && apt-get install fontconfig -y
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:3.1-buster-slim AS build
WORKDIR /src
COPY ["Convert-Excel-to-PDF.csproj", "."]
RUN dotnet restore "./Convert-Excel-to-PDF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Convert-Excel-to-PDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Convert-Excel-to-PDF.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Convert-Excel-to-PDF.dll"]

A complete working example of converting an Excel document to PDF in Debian Linux Docker container is present on this GitHub page.

RHEL - Red Hat Enterprise Linux

Use the following Dockerfile to convert an Excel document to PDF in RHEL Linux.

FROM registry.access.redhat.com/ubi8/dotnet-31-runtime AS base
USER root
RUN yum -y install fontconfig --disablerepo=epel
WORKDIR /

FROM registry.access.redhat.com/ubi8/dotnet-31 AS build
WORKDIR /src
COPY ["Convert-Excel-to-PDF.csproj", ""]
RUN dotnet restore "./Convert-Excel-to-PDF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Convert-Excel-to-PDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Convert-Excel-to-PDF.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Convert-Excel-to-PDF.dll"]

A complete working example of converting an Excel document to PDF in RHEL Linux Docker container is present on this GitHub page.

Ubuntu

Use the following Dockerfile to convert an Excel document to PDF in Ubuntu Linux.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-bionic AS base
RUN apt-get update -y && apt-get install fontconfig -y
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /src
COPY ["Convert-Excel-to-PDF.csproj", ""]
RUN dotnet restore "./Convert-Excel-to-PDF.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Convert-Excel-to-PDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Convert-Excel-to-PDF.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Convert-Excel-to-PDF.dll"]

A complete working example of converting an Excel document to PDF in Ubuntu Linux Docker container is present on this GitHub page.

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.