How to create Docker file for HTML to RTF .Net


    The following is a project file for the .NET Core application with added Docker support.

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SautinSoft.HtmlToRtf" Version="*" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="*" />
<PackageReference Include="System.Drawing.Common" Version="*" />
<PackageReference Include="System.Security.Permissions" Version="*" />
</ItemGroup>

</Project>

To configure or customize a Docker image, you'll need to edit the Dockerfile.

When creating HTML to RTF files with images, System.Drawing.Common is used to process image data, which requires a libgdiplus package on a Linux container.

When creating RTF/DOCX files, the font files need to be present in the container. The official Linux images for .NET won't have any fonts installed. So, you'll need to copy the necessary font files to the Linux container, or install a font package like ttf-mscorefonts-installer, or add them as Private Fonts.

The following example shows how you can create HTML to RTF files from Docker containers and configure Docker images with Dockerfile.

DockerFile:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app

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

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

FROM base AS final

# Update package sources to include supplemental packages (contrib archive area).
RUN sed -i 's/main/main contrib/g' /etc/apt/sources.list

# Downloads the package lists from the repositories.
RUN apt-get update

# Install System.Drawing.Common dependency.
RUN apt-get install -y libgdiplus

# Install Microsoft TrueType core fonts.
RUN apt-get install -y ttf-mscorefonts-installer

# Or install Liberation TrueType fonts.
# RUN apt-get install -y fonts-liberation

# Or some other font package...

WORKDIR /app
COPY --from=publish /app/publish.

Thanks.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.