Convert an Excel document to PDF in Linux Docker
8 Dec 20236 minutes to read
Docker is an open platform for developing, shipping and running applications. You can use Essential XlsIO in Docker container to create, read, write and convert Microsoft Excel documents into various formats. From this page, you can learn how to convert an Excel document to PDF in Linux Docker using Syncfusion XlsIO library (Essential XlsIO).
Steps to convert an Excel document to PDF in Linux Docker
Step 1: Create a new Core Console application.
Step 2: Name the project.
Step 3: Install the below NuGet packages as a reference to your project from 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.Pdf;
using Syncfusion.XlsIORenderer;
Step 5: Add the following code snippet in Program.cs file.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream excelStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Create the FileStream to save the converted PDF.
FileStream pdfStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite);
pdfDocument.Save(pdfStream);
}
Step 6: Add Docker support to that application by clicking Add -> Docker Support.
Step 7: Choose Linux option in order to run the application in Linux Docker container.
Step 8: Open the Dockerfile to see the default Docker commands that are shown below.
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 8: Select Docker option 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 follows.
Dockerfile Examples
The following examples demonstrate how the Docker file should be configured in order to convert an Excel document to PDF in different Linux distributions.
Alpine
You can use the below 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
You can use the below 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
You can use the below 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
You can use the below 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.