Fixing the ‘The type initializer for Syncfusion.EJ2.PdfViewer.PdfiumNative threw an exception’ Error in Linux hosted ASP.NET Core Application

Manvel Ghazaryan
2 min readMar 12, 2024

I recently encountered the The type initializer for ‘Syncfusion.EJ2.PdfViewer.PdfiumNative’ threw an exception error in my Linux hosted ASP.NET Core application. Despite finding several helpful posts, it took a combination of their suggestions to resolve the issue. In this post, I’ll share the consolidated steps that led to my solution.

The Issue

The error was being thrown while trying to instantiate an instance of PdfRenderer class

IMemoryCache cache = ...
var pdfViewer = new PdfRenderer(cache);

Interestingly, the error didn’t appear on my local Windows dev machine or the Windows-based Azure App Service test environment. However, it surfaced in the DEV/QA environments hosted on AKS as Linux containers.

A quick Google search led me to a Syncfusion guide detailing the issue’s root cause and solution. However, merely adding the Linux pdfium library and the Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux NuGet package wasn’t enough. Further research brought me to a support ticket suggesting manual installation of pdfium dependencies. Combining these steps finally resolved the issue. What follows are the detailed steps.

The Solution

Following the guide, download pdfium and copy corresponding file to the solution. In my case issue is with Linux, so I copied over Linux version

Make sure to set it to Copy To Output Directory

<ItemGroup>
<None Update="Pdfium\libpdfium.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Set ReferencePath on PdfRenderer

public MyController(IHostEnvironment hostEnvironment)
{
PdfRenderer.ReferencePath =
System.IO.Path.Combine(hostEnvironment.ContentRootPath, "Pdfium");
}

Make sure Linux specific package is referenced

<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux" 
Version="24.1.41"
Condition="$([MSBuild]::IsOsPlatform('Linux'))" />
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows"
Version="24.1.41"
Condition="$([MSBuild]::IsOsPlatform('Windows'))" />

Install pdfium dependencies manually. Since I'm using Dockerfile, I'm adding this step there

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev

COPY bin/Release/net6.0/ MyApp/
WORKDIR MyApp/
EXPOSE 5000
ENV ASPNETCORE_URLS=http://*:5000
ENTRYPOINT ["dotnet", MyApp.dll"]

That resolved the exception. Happy coding !

--

--