The type initializer for 'Syncfusion.EJ2.PdfViewer.PdfiumNative' threw an exception.

On windows the program work perfectly, but not on a linux docker.

I use that DLL : Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux

.net core 5.02

I gave write access in my docker to be sure.

And i always get that error 
 'Syncfusion.EJ2.PdfViewer.PdfiumNative' on that line :  jsonResult = pdfviewer.Load(stream, jsonData);

Here is my docker file :

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base

WORKDIR /app

EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build

WORKDIR /src

COPY ["ServerPdfViewer/ServerPdfViewer.csproj", "ServerPdfViewer/"]

RUN dotnet restore "ServerPdfViewer/ServerPdfViewer.csproj"

COPY . .

WORKDIR "/src/ServerPdfViewer"

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

FROM build AS publish

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

FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .


Here is my function

        public IActionResult Load([FromBody] Dictionary<string, string> jsonData)

        {

            PdfRenderer pdfviewer = new PdfRenderer(_cache);

            //PdfRenderer.ReferencePath = _hostingEnvironment.ContentRootPath;

            MemoryStream stream = new MemoryStream();

            object jsonResult = new object();

            if (jsonData != null && jsonData.ContainsKey("document"))

            {

                if (bool.Parse(jsonData["isFileName"]))

                {

                    string documentPath = GetDocumentPath(jsonData["document"]);


                    if (!string.IsNullOrEmpty(documentPath))

                    {

                        byte[] bytes = System.IO.File.ReadAllBytes(documentPath);

                        stream = new MemoryStream(bytes);

                    }

                    else

                    {

                        string fileName = jsonData["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0];

                        if (fileName == "http" || fileName == "https")

                        {

                            try

                            {

                                WebClient WebClient = new WebClient();

                                byte[] pdfDoc = WebClient.DownloadData(jsonData["document"]);

                                stream = new MemoryStream(pdfDoc);

                            }

                            catch (Exception ex)

                            {

                                var a = ex;

                            }

                        }

                        else

                        {

                            return this.Content(jsonData["document"] + " is not found");

                        }


                    }

                }

                else

                {

                    byte[] bytes = Convert.FromBase64String(jsonData["document"]);

                    stream = new MemoryStream(bytes);

                }

            }

            try

            {

                jsonResult = pdfviewer.Load(stream, jsonData);

            }

            catch (Exception ex)

            {

                var a = ex;

            }

            return Content(JsonConvert.SerializeObject(jsonResult));

        }


17 Replies 1 reply marked as answer

VS Vasugi Sivajothi Syncfusion Team February 1, 2021 09:35 AM UTC

Hi Frédéric, 
 
 
Thanks for contacting Syncfusion support. 
 
 
Our Pdfviewer service depends on the open-source library PDFIUM, To work with PDFIUM in the LINUX environment we need to add the following commands in the docker file to install dependencies.  
RUN ln -s /lib/x86_64-linux-gnu/libdl-2.24.so /lib/x86_64-linux-gnu/libdl.so  
# install System.Drawing native dependencies  
RUN apt-get update && apt-get install -y --allow-unauthenticated libgdiplus libc6-dev libx11-dev  
RUN ln -s libgdiplus.so gdiplus.dll 
 
we have ensured our PDF Viewer service in .NET 5.0 Framework with Linux docker container and it’s working fine. For your reference herewith we have attached the service project along with docker file and it can be downloaded from the following link,  
 
 
 
 
Kindly try it and let us know if you face any problem with this. 
 
Regards, 
Vasugi

Marked as answer

FR Frédéric February 1, 2021 11:55 AM UTC

Yeah ! that's perfect ! it work now.

Thank you for your fast reply


TA Tuan Ardouin February 1, 2021 12:07 PM UTC

Hello, 

I still have the same problem on my AWS Lamba. 

Do you have any news regarding this problem ?

https://www.syncfusion.com/forums/151628/the-type-initializer-for-syncfusion-ej2-pdfviewer-pdfiumnative-threw-an-exception

Thanks,
Tuan


VS Vasugi Sivajothi Syncfusion Team February 2, 2021 05:57 PM UTC

Hi Tuan, 
 
Sorry for the Inconvenience caused. 
We are currently working on your issue. We will analyze and update more details on February 4 2021. 
 
Regards, 
Vasugi  



VS Vasugi Sivajothi Syncfusion Team February 4, 2021 02:17 PM UTC

Hi Tuan, 
 
Sorry for the inconvenience. 
 
We are currently working on analyzing the way for resolve the issue in the AWS environment with high priority, we will update the further details on February 8th 2021. 
 
Regards,
Vasugi
 



TA Tuan Ardouin February 9, 2021 02:55 PM UTC

Hello,

Do you have any update ?

If you need more time or if I can be of any assistance please let me know.

Tuan


VS Vasugi Sivajothi Syncfusion Team February 10, 2021 05:02 PM UTC

Hi Tuan, 
 
Thanks for your update. 
 
Currently, we are facing some permission problems in the AWS environment while publishing the project. We are working on this with high priority and we will update the further details on February 12th, 2021.  
 
 
Regards, 
Vasugi 
 



VS Vasugi Sivajothi Syncfusion Team February 16, 2021 01:59 AM UTC

Hi Tuan, 
  
  
Sorry for the inconvenience. 
  
  
As we mentioned earlier, we have facing complexity in the AWS environment while publishing the project. So we will update further details on February  17, 2021.  
  
  
Regards, 
Vasugi 



TA Tuan Ardouin February 16, 2021 10:45 AM UTC

Hello,

Thank you for the updates.

Once again, if you need any assistance I would be happy to try things on my side.

Tuan


VS Vasugi Sivajothi Syncfusion Team February 17, 2021 05:08 PM UTC

Hi Tuan, 
  
  
Thank you for your update. 
  
Currently, we have facing gdiplus exception while publishing the project. We have manually copied the dependency dll but still the same issue occurs. So, we will upgrade the .net core 5.0 version. We will check further on this and update you with more details on February 19, 2021. 
  
  
Regards, 
Vasugi 



TA Tuan Ardouin February 18, 2021 11:21 AM UTC

Hello,

Thanks again for your updates.

Would you share your current lambda directory you're trying to push ?

Thanks


VS Vasugi Sivajothi Syncfusion Team February 19, 2021 04:42 PM UTC

Hi Tuan, 
 
We have created the simple sample by exporting the PDF document to image in AWS .NET Core 5.0 version. But we are facing the “Couldn’t find valid bootstrap(s), Runtime.InvalidEntrypoint” error. So, we are currently checking on the AWS forum for resolving the issue on our end, we will update the details once we resolve the issue on our end. In the meanwhile, can you please let us know whether you have faced the same issue in your sample? 
 
 
Regards, 
Vasugi 



TA Tuan Ardouin February 24, 2021 07:03 PM UTC

Hello,

I'm encountering the same problem. Did you adapt your docker to run on a Windows environment ?

Thanks,
Tuan


TA Tuan Ardouin February 26, 2021 12:22 PM UTC

Hello,

I've almost done it.

I've managed to create the API endpoint for /Load but I'm stuck on the /RenderPdfPages. The problem is that each call to those endpoint is handled by a different Lambda, so when I try to "  pdfExportImage.GetPage(jsonObject) " I get the error "Document Reference pointer does not exist in the cache", which is normal because Lambda can't find the previously loaded PDF in memory.

My idea was to fetch the PDF from S3 when rendering the PDF but I would need an object key to do that. I was hoping that the uniqueId in the body of the /Load request corresponded to the name of the file but it's not, it's a random id as far as I can see.

Is there a way to either :
- Set the uniqueId when calling /Load
- Modify the body of the request when calling /RenderPdfPages

I can share my lambda if you wish to try it yourself. 

Thanks,
Tuan


VS Vasugi Sivajothi Syncfusion Team February 26, 2021 03:27 PM UTC

Hi Tuan, 
 
Thanks for your update. 
 
We have to use the IMemoryCache to store some document data for rendering the PDF document. If the scaling is enabled, then the request will send to the different server machines, but in this, the cache data will not available. Since the memory cache will store them in RAM. For overcoming this we have provided the distributed cache (Redis) support for retrieving the stored data with different server machines, so the PDF document will get rendered in our PDF Viewer control. We have implemented the Redis cache for achieving load balancing for our web service. If you are using multiple instances by scaling in a cloud environment, we recommend using the Redis cache 
 
Kindly follow the below steps for using the Redis Cache,      
   
STEP 1: To use Redis cache in PDF Viewer service, install the following NuGet package,        
                             Microsoft.Extensions.Caching.Redis        
       
STEP 2: To configure the Redis cache in the PDF Viewer service, you have to add the following changes in Startup. cs        
        
services.AddMemoryCache();        
services.AddDistributedRedisCache(options =>        
{        
  options.Configuration = Configuration.GetConnectionString("Redis");                        
});        
   
   
STEP 3: We have to use IDistributedCache instead of IMemoryCache in PdfViewerController.cs. Please find the PDF Viewer service sample and code snippet below.        
       
       
PdfViewerController.cs       
private IDistributedCache _cache;       
public PdfViewerController(IHostingEnvironment hostingEnvironment, IDistributedCache cache)       
        {       
            _hostingEnvironment = hostingEnvironment;       
            _cache = cache;       
        }       
   
 
Regards, 
Vasugi 



MP Michael Pongan December 21, 2021 09:52 AM UTC

Hi there,


Evaluating PdfViewer for huge project.


We tried this solution but still facing the same issue when deployed to linux docker.


Here's our docker file. Let me know if I missed anything.


FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /var/local/app

# setup directory structure to work with VS project references
RUN mkdir /var/local/app/API
RUN mkdir /var/local/app/Shared

# Copy csproj and restore as distinct layers
# hiding project specific

# Copy everything else and build
# hiding project specific
RUN dotnet publish -c Release --self-contained false -o /var/local/app/out/content # Set the emty directory as a mount point for K8 secrets containing TLS Certs RUN mkdir /var/local/app/out/content/security ################################################### ### Build the final container ### https://hub.docker.com/_/microsoft-dotnet-aspnet FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim # https://www.syncfusion.com/forums/166222/pdf-viewers-service-is-throwing-an-exception-the-type-initializer-for-syncfusion-ej2 RUN ln -s /lib/x86_64-linux-gnu/libdl-2.24.so /lib/x86_64-linux-gnu/libdl.so # install System.Drawing native dependencies https://www.syncfusion.com/forums/162767/sfpdfviewer-component-erreur-syncfusion-ej2-pdfviewer-pdfiumnative-threw-an-exception-on RUN apt-get update && apt-get install -y --allow-unauthenticated libgdiplus libc6-dev libx11-dev xvfb libfontconfig wkhtmltopdf openssl libssl-dev RUN ln -s libgdiplus.so gdiplus.dll WORKDIR /var/local/app COPY --from=build-env /var/local/app/out/content . ENTRYPOINT ["dotnet", "5fkdjfctApi.dll"]


VS Vasugi Sivajothi Syncfusion Team December 22, 2021 08:53 AM UTC

Hi Michael, 
  
In our PDF Viewer, we have embedded the Pdfium rendering engine in our PDF Viewer for robust rendering. So, Pdfium dll will be generated on runtime within your project location. We suspect that the issue is due to the missing of the libpdfium.so assembly in the published Docker environment. So, we suggest you place the pdfium assembly in the project location and enabling the copy to Output Directory property to copy always and to publish the same. To work with PDFIUM in the LINUX environment we need to add the following commands on top in the docker file to install the dependencies.    
   
Commands:   
 
RUN ln -s /lib/x86_64-linux-gnu/libdl-2.24.so /lib/x86_64-linux-gnu/libdl.so    
# install System.Drawing native dependencies    
RUN apt-get update && apt-get install -y --allow-unauthenticated libgdiplus libc6-dev libx11-dev    
RUN ln -s libgdiplus.so gdiplus.dll    
   
 
 
We have shared the simple docker code snippet for your reference. Please refer to the below link. 
 
And please install only  Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux NuGet package in your environment. For more information, please refer to the below link. 
 
 
Kindly try this and let us know if you have any concerns about this. 
 
Regards, 
Vasugi. 


Loader.
Up arrow icon