PDF compression process problem in Docker

I want to compress the pdf file I uploaded via the web interface and save it on azure blob storage. When I want to do this on my own computer, it works without any problems. However, when I run my code on docker, it does not compress pdf.


I am sharing the code I used below.


public async Task<IActionResult> SaveComment(Comment request)
        {
            try
            {
                if (request.FileUpload != null && request.FileUpload.Length > 0)
                {
                    string base64 = "";

                    Stream ss = request.FileUpload.OpenReadStream();

                    PdfLoadedDocument loadedDocument = new PdfLoadedDocument(ss);

                    PdfCompressionOptions compressionOptions = new PdfCompressionOptions();
                    compressionOptions.CompressImages = true;
                    compressionOptions.ImageQuality = 30;
                    compressionOptions.OptimizeFont = true;
                    compressionOptions.OptimizePageContents = true;
                    compressionOptions.RemoveMetadata = true;


                    //Flatten form fields in the PDF document
                    if (loadedDocument.Form != null)
                        loadedDocument.Form.Flatten = true;

                    //Flatten all the annotations in the PDF document
                    foreach (PdfPageBase page in loadedDocument.Pages)
                    {
                        if (page.Annotations != null)
                            page.Annotations.Flatten = true;
                    }

                    loadedDocument.Compress(compressionOptions);

                    MemoryStream stream = new MemoryStream();

                    loadedDocument.Save(stream);

                    loadedDocument.Close(true);


                    base64 = Convert.ToBase64String(stream.ToArray());



                    request.FormFileBase64 = base64;
                    request.FilePath = request.FileUpload.FileName;
                    request.FileUpload = null;
                }

                var saveResponse = await _commentService.SaveAsync(request);
                return Json(saveResponse);
            }
            catch (Exception ex)
            {
                Dispose(true);
                return Json(ex.Message);
            }
            finally
            {
                Dispose(true);
            }
        }

5 Replies

IJ Irfana Jaffer Sadhik Syncfusion Team May 9, 2025 12:33 PM UTC

Hi Yigit,


We suspect that the issue you're experiencing with compressing PDF files using Docker is likely due to missing SkiaSharp Linux dependency packages in your current Linux environment.


To resolve this issue, please follow these steps:


Install the following NuGet package in your application:

Package Name: SkiaSharp.NativeAssets.Linux

Version: 3.116.1

NuGet Link: SkiaSharp.NativeAssets.Linux Version 3.116.1

This package includes the necessary native assets for SkiaSharp to function properly in Linux environments.


If you need assistance with the installation process or if the issue persists after following these steps, please let us know. Additionally, please note that this solution is intended for Linux environments. If the issue continues unresolved, we request that you provide a modified sample, the input document, as well as environmental details and steps to replicate the issue.


Regards,

Irfana J.


Attachment: PDFCompression_2d0abcf5.zip


YI Yigit May 9, 2025 06:00 PM UTC

Hello Irfana,

Thank you for your guidance on solving the PDF compression issue in our Docker environment. I followed your instructions and installed the recommended NuGet package SkiaSharp.NativeAssets.Linux (Version 3.116.1) in my application, but the problem is not solved.


Additionally, I created a sample project to demonstrate the problem and shared it in the link below:

https://matasys.com/FileUpload.MVC.zip

Please let me know if you need any further details such as proof of entry, environmental details or steps to take to replicate the issue. Thank you for your support in resolving this matter.




Best regards,



IJ Irfana Jaffer Sadhik Syncfusion Team May 12, 2025 02:10 PM UTC

Hi Yigit,


We are currently investigating the reported behavior using the sample provided on our side and we will share the further validation details on May 14th,2025. However, it is crucial to receive the correct input document to accurately determine the root cause of the issue.

Please be assured that Syncfusion adheres to strict confidentiality regarding all information shared by our customers. We have a Non-Disclosure Agreement (NDA) in place to ensure that any documents you share will only be used to replicate and resolve the issue, and will not be disclosed to any third party.


Regards,

Irfana J.



IJ Irfana Jaffer Sadhik Syncfusion Team May 14, 2025 10:13 AM UTC

HI Yigit,

Upon further analysis, we were able to reproduce the reported issue on our end. The SkiaSharp Linux package is required for compressing PDF documents in the Linux environment. However, when installing the SkiaSharp package, the native SkiaSharp library failed to include the libfontconfig.so.1 dependency, which is causing the issue. We can resolve this by installing the missing dependency in the Dockerfile. We have attached a sample, screenshots and the docker commands for your reference.
 
Image_4549_1747217590385

Docker command:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
RUN apt-get update && \
    apt-get install -y libfontconfig1 && \
    rm -rf /var/lib/apt/lists/*
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


Regards,
Irfana J.


IJ Irfana Jaffer Sadhik Syncfusion Team May 14, 2025 10:13 AM UTC

HI Yigit,

Upon further analysis, we were able to reproduce the reported issue on our end. The SkiaSharp Linux package is required for compressing PDF documents in the Linux environment. However, when installing the SkiaSharp package, the native SkiaSharp library failed to include the libfontconfig.so.1 dependency, which is causing the issue. We can resolve this by installing the missing dependency in the Dockerfile. We have attached a sample, screenshots and the docker commands for your reference.
 
Image_4549_1747217590385

Docker command:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
RUN apt-get update && \
    apt-get install -y libfontconfig1 && \
    rm -rf /var/lib/apt/lists/*
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


Regards,
Irfana J.

Loader.
Up arrow icon