Essential® DocIO in Docker

24 Jul 20269 minutes to read

Docker is an open platform for developing, shipping and running applications. You can use Essential® DocIO in a Docker container to create, read, write, and convert Microsoft Word documents into various formats. From this page, you can learn how to convert a Word document to PDF in a Linux Docker container using the .NET Word Library (Essential® DocIO).

Prerequisites

The following prerequisites are required before you begin:

  • .NET SDK 8.0
  • Docker Desktop (Windows/macOS) or Docker Engine (Linux) installed and running
  • A valid Syncfusion® license key. Refer to this licensing guide to generate and register a key.
  • Visual Studio 2022 or later with the .NET desktop development and .NET Core cross-platform development workloads (for the “Add Docker Support” steps in Visual Studio)

Steps to convert a Word document to PDF in Linux Docker

Step 1: Create a new .NET Core Console application targeting .NET 8.0.

Create console app
Name console app

Step 2: Install the below NuGet packages as a reference to your project from NuGet.org.

Install DocIO Renderer NuGet package
Install Skiasharp NuGet package

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, you also have to add a reference to the Syncfusion.Licensing assembly and include a license key in your project. Refer to this licensing guide to learn how to register the license key in your application.

Step 3: Include the following namespaces in the Program.cs file.

using System.IO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

Step 4: Add the following code snippet to the Program.cs file.

//Open the file as Stream
using (FileStream docStream = new FileStream(@"Adventure.docx", FileMode.Open, FileAccess.Read))
{
    //Loads file stream into Word document
    using (WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic))
    {
        //Instantiation of DocIORenderer for Word to PDF conversion
        using (DocIORenderer render = new DocIORenderer())
        {
            //Converts Word document into PDF document
            using (PdfDocument pdfDocument = render.ConvertToPDF(wordDocument))
            {
                //Saves the PDF file
                using (FileStream outputStream = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    pdfDocument.Save(outputStream);
                }
            }
        }
    }
}

Step 5: Add Docker support to the application by right-clicking the project in Solution Explorer and selecting Add > Docker Support.

Add Docker support to that console app

Step 6: Choose the Linux option to run the application in a Linux Docker container.

Choose Linux option

Step 7: Open the Dockerfile, which contains the following default Docker commands:

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

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

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

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

Step 8: Place a copy of Adventure.docx in the project output folder (or update the path in Program.cs), then select the Docker option and run the application.

Run console app

A complete working example of converting Word document to PDF in Linux Docker container can be downloaded from GitHub.

Finally, you will get the converted PDF document as follows.

Output PDF document

Dockerfile Examples

The following examples demonstrate how the Docker file should be configured in order to convert a Word document to PDF in different Linux distributions.

Alpine

You can use the below Dockerfile to convert a Word document to PDF in Alpine Linux.

FROM mcr.microsoft.com/dotnet/runtime:8.0-alpine AS base
RUN apk update && apk add --no-cache fontconfig ttf-dejavu
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["WordToPDFDockerSample.csproj", "."]
RUN dotnet restore "./WordToPDFDockerSample.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

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

A complete working example of converting Word document to PDF in Alpine Linux Docker container can be downloaded from GitHub.

CentOS

You can use the below Dockerfile to convert a Word document to PDF in CentOS Linux.

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
RUN apt-get update -y && apt-get install libfontconfig -y
WORKDIR /src

COPY ["WordToPDFDockerSample.csproj", "."]
RUN dotnet restore "WordToPDFDockerSample.csproj"

COPY . .
RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM centos:8

RUN dnf install -y https://packages.microsoft.com/config/centos/8/prod.repo \
    && dnf install -y dotnet-runtime-8.0 \
    && dnf clean all

RUN dnf install -y fontconfig

WORKDIR /app

COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "WordToPDFDockerSample.dll"]

A complete working example of converting a Word document to PDF in a CentOS Linux Docker container can be downloaded from GitHub.

Debian

You can use the below Dockerfile to convert a Word document to PDF in Debian Linux.

FROM mcr.microsoft.com/dotnet/runtime:8.0-bookworm-slim AS base
RUN apt-get update -y && apt-get install fontconfig -y
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
WORKDIR /src
COPY ["WordToPDFDockerSample.csproj", "."]
RUN dotnet restore "./WordToPDFDockerSample.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

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

A complete working example of converting Word document to PDF in Debian Linux Docker container can be downloaded from GitHub.

Fedora

You can use the below Dockerfile to convert a Word document to PDF in Fedora Linux.

FROM fedora:latest

Run dnf install dotnet-sdk-8.0 -y
RUN dnf install dotnet-runtime-8.0 -y

RUN dnf install fontconfig -y

WORKDIR /app

COPY ["WordToPDFDockerSample.csproj", "."]
RUN dotnet restore "WordToPDFDockerSample.csproj"
COPY . .

RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

ENTRYPOINT ["dotnet", "WordToPDFDockerSample.dll"]

A complete working example of converting Word document to PDF in Fedora Linux Docker container can be downloaded from GitHub.

RHEL - Red Hat Enterprise Linux

You can use the below Dockerfile to convert a Word document to PDF in RHEL Linux.

FROM registry.access.redhat.com/ubi8/dotnet-80-runtime AS base
USER root
RUN microdnf install -y fontconfig \
    && microdnf clean all
WORKDIR /app

FROM registry.access.redhat.com/ubi8/dotnet-80 AS build
WORKDIR /src
COPY ["WordToPDFDockerSample.csproj", ""]
RUN dotnet restore "./WordToPDFDockerSample.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

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

A complete working example of converting Word document to PDF in RHEL Linux Docker container can be downloaded from GitHub.

Ubuntu

You can use the below Dockerfile to convert a Word document to PDF in Ubuntu Linux.

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

FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build
WORKDIR /src
COPY ["WordToPDFDockerSample.csproj", "."]
RUN dotnet restore "./WordToPDFDockerSample.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WordToPDFDockerSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WordToPDFDockerSample.csproj" -c Release -o /app/publish

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

A complete working example of converting Word document to PDF in Ubuntu Linux Docker container can be downloaded from GitHub.

Looking for the full .NET Word Library overview, features, pricing, and documentation? Visit the .NET Word Library page.

An online sample to convert a Word document to PDF is also available.

See Also