Aspose.Words Conversion Throw Error in Docker

This documentation addresses common errors encountered when using Aspose.Words for document conversion (e.g., to PDF, JPEG, PNG) in a .NET Core 2.0 application running inside a Docker container. The errors typically relate to SkiaSharp type initializers and occur only in Docker environments (not in local IIS Express).

Error Scenarios

1. PDF Conversion Error

The type initializer for ‘SkiaSharp.SKManagedStream’ threw an exception.

2. JPEG/PNG Conversion Error

The type initializer for ‘SkiaSharp.SKImageInfo’ threw an exception.

Root Cause

The errors stem from missing native dependencies required by the SkiaSharp library, which Aspose.Words uses for rendering graphics. Docker containers (especially lightweight Linux-based images) often lack these dependencies by default.

Solution Steps

1. Install Required Dependencies in Docker

Add the necessary native libraries to your Docker image. These include:

  • libgdiplus (for graphics operations)
  • libfontconfig1 (for font rendering)
  • libc6-dev (C library utilities)

Update your Dockerfile to include these dependencies:

# Use a .NET Core 2.0 compatible base image
FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build-env
# Install dependencies
RUN apt-get update && \
apt-get install -y \
libfontconfig1 \
libgdiplus \
libc6-dev \
&& rm -rf /var/lib/apt/lists/*
# Rest of your Dockerfile (build steps, entrypoint, etc.)

2. Include SkiaSharp Native Assets

Ensure the SkiaSharp.NativeAssets.Linux NuGet package is installed in your project. This package provides Linux-specific binaries required for SkiaSharp to work in Docker.

dotnet add package SkiaSharp.NativeAssets.Linux –version 2.88.0
Note: Use a version compatible with your project and Aspose.Words.

3. Verify Docker Base Image

Avoid using minimal Docker images (e.g., alpine), as they lack essential dependencies. Use a Debian/Ubuntu-based image instead:

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-bionic

4. Update Aspose.Words and SkiaSharp

Ensure you’re using the latest compatible versions of:
  • Aspose.Words
  • SkiaSharp
  • SkiaSharp.NativeAssets.Linux
Check for version conflicts in your .csproj file.

5. Font Configuration

If fonts are missing (common in headless Docker environments), explicitly install fonts or copy them to the container:

# Example: Install DejaVu fonts
RUN apt-get install -y ttf-dejavu

6. Test in Debug Mode

Run the Docker container in debug mode to capture detailed error logs:

docker run -it –rm your-image-name

Additional Notes

  • .NET Core 2.0 Compatibility: .NET Core 2.0 is outdated and may have compatibility issues with newer libraries. Consider upgrading to a supported .NET version.
  • Aspose.Words Licensing: Ensure your license is properly embedded in the Docker container.
  • Docker Cache: Rebuild the Docker image without cache if changes don’t take effect:
docker build --no-cache -t your-image-name If the issue persists, contact Aspose Support with your Dockerfile, error logs, and a reproducible sample.

Need Help With .Net Development?

Work with our skilled .Net developers to accelerate your project and boost its performance.

Support On Demand!