Skip to content

About

Document generation in Docker with .NET: the fonts a Linux container needs, and a rendered page as proof they are there. GroupDocs.Assembly for .NET sample.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Document generation in Docker with .NET

build NuGet .NET License: MIT

If a document generated in a container looks wrong, it is almost always the fonts: the .NET runtime images ship with none. Install fontconfig and fonts-liberation, then render a page with DocumentAssembler.GeneratePreview to prove they are there.

Document generation moves to Linux the moment it goes into a container, a Kubernetes job or a function. The code does not change. The image does — and the single thing that catches everybody is fonts: the .NET runtime images ship without any, nothing fails when they are missing, and the output is quietly laid out with a substitute.

This sample makes that visible. It assembles a report, renders its pages to PNG — GeneratePreview is new in 26.11 — and lists the fonts the runtime can actually see.

docker build -t document-generation-dotnet .
docker run --rm document-generation-dotnet fonts

a rendered page of the report

Read this with

Resource What it adds
Blog: Generating documents in Docker with .NET the whole story, including what a missing font does to a page
Docs: Running in Docker and serverless the reference page
This repository the code that runs

Quick start

git clone https://github.com/groupdocs-assembly/document-generation-docker-net.git
cd document-generation-docker-net

dotnet run --project src/DockerReports -- report     # assemble DOCX and PDF
dotnet run --project src/DockerReports -- preview    # render the pages to PNG
dotnet run --project src/DockerReports -- fonts      # what the runtime can see

In a container:

docker build -t document-generation-dotnet .
docker run --rm -v "$PWD/output:/work/output" document-generation-dotnet report
docker run --rm -v "$PWD/output:/work/output" document-generation-dotnet preview output/orders.docx
docker run --rm document-generation-dotnet fonts

The three lines that matter

RUN apt-get update \
 && apt-get install -y --no-install-recommends fontconfig fonts-liberation \
 && rm -rf /var/lib/apt/lists/*

fontconfig is what finds fonts at all; fonts-liberation provides metric-compatible substitutes for Arial, Times New Roman and Courier New, which is what most templates ask for. Without them fonts prints total: 0 font file(s) and the renderer falls back to whatever it can find — line breaks move, tables overflow, and a page that was one page becomes two.

A template that uses other fonts needs those fonts in the image as well. Copy them into /usr/share/fonts and run fc-cache -f, or point the assembler at a folder of your own.

Rendering is the test

A DOCX hides a font problem until somebody opens it on a desktop. Rendering it does not:

PreviewOptions previewOptions = new PreviewOptions(
    pageNumber => File.Create($"page{pageNumber}.png"),
    (pageNumber, pageStream) => pageStream.Dispose());

previewOptions.PreviewFormat = PreviewFormats.PNG;
previewOptions.Resolution = 120;           // dots per inch, 96 by default

new DocumentAssembler().GeneratePreview(documentPath, previewOptions);

GeneratePreview came with 26.11 and takes a path or a stream. Put one call in the smoke test of your image and a missing font becomes a failing build instead of a support ticket.

Serverless

The same image works as the base of an Azure Functions custom handler, an AWS Lambda container image or a Cloud Run job. Three things to plan for:

  • Cold start. The first assembly in a process pays for loading the engine. Keep the function warm, or accept the first call being slower.
  • Writable space. Assemble to streams rather than to files — nothing in this sample needs a writable disk except the sample output itself.
  • The licence. Ship it as a secret, not in the image, and apply it once per process.

Layout

src/DockerReports/
  Program.cs     command line
  Reports.cs     assemble, preview and the font check
Dockerfile       runtime image with fonts
templates/Customer orders.docx
data/Customers.json

Every call mirrors an example of the product's examples repository (GroupDocs.Assembly-for-.NET).

Notes

  • Licence. Without one the library runs in evaluation mode and the output carries evaluation marks (temporary licence).
  • Frameworks. The sample targets .NET 8; the package also builds for .NET Framework 4.0 Client Profile, 4.6.2 and 4.8, .NET Standard 2.0, .NET 6 and .NET 10.

Common questions

Why does my generated document look different in Docker? No fonts in the image. Text is laid out with a substitute, so line breaks, column widths and page counts move — and nothing throws.

Which packages do I need? fontconfig to make fonts discoverable at all, plus fonts-liberation, which is metric-compatible with Arial, Times New Roman and Courier New.

How do I add a corporate font? Copy it into /usr/share/fonts and run fc-cache -f.

How do I catch a missing font in CI? Render a page during the image build and compare it with a reference PNG. A DOCX hides the problem because layout is decided by whatever opens it.

Does this work in Azure Functions or AWS Lambda? Yes, on the same base image. Assemble to streams, apply the licence once per process, and keep an instance warm if the first request is user-facing.

Related samples

License

MIT, see LICENSE. GroupDocs.Assembly itself is a commercial library with its own licence terms.

About

Document generation in Docker with .NET: the fonts a Linux container needs, and a rendered page as proof they are there. GroupDocs.Assembly for .NET sample.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages