linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

Unable to convert Excel to PDF with Ubuntu/Docker/NGINX

Using GemBox.Spreadsheet, requested Excel files are rendered beautifully as PDFs on my Windows 11 (IIS Express) development machine; however, requesting the same PDFs from a Ubuntu/Docker/NGINX web server results in an HTTP 500 error.

The following C# snippet returns a byte array to a calling method which, subsequently, returns the byte array as a FileContentResult to a web client:

    private byte[] FileToByteArray(string fileName)
    {
        // The following uses GemBox.Spreadsheet to convert from Excel spreadsheet to PDF.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
        //  1. Load XLS or XLSX file into ExcelFile object.
        ExcelFile workbook = ExcelFile.Load(fileName + ".xlsx");
        //  2. Iterate each worksheet in the selected workbook to fit each worksheet to a single printable page width.
        foreach (var worksheet in workbook.Worksheets)
        {
            worksheet.PrintOptions.FitWorksheetWidthToPages = 1;
            //worksheet.PrintOptions.FitWorksheetHeightToPages = 1;
        }
        //  3. Save ExcelFile object to PDF file.
        workbook.Save(fileName + ".pdf", new PdfSaveOptions() { SelectionType = GemBox.Spreadsheet.SelectionType.EntireFile });
        //  4. Read the PDF file into a byte array.
        byte[] bytes = System.IO.File.ReadAllBytes(fileName + ".pdf");
        //  5. Delete the Excel source file from the web server.
        System.IO.File.Delete(fileName + ".xlsx");
        //  6. Delete the PDF file from the web server.
        System.IO.File.Delete(fileName + ".pdf");

        return bytes;
    }

For reference, the following provides the current contents of my “Dockerfile” script:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.3 AS base
WORKDIR /app

#EXPOSE 80
#EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.201 AS build
WORKDIR /src
COPY . WebApplication/
RUN dotnet restore WebApplication/WebApplication.csproj

#COPY . .

WORKDIR /src/WebApplication
RUN dotnet build WebApplication.csproj -c Release -o /app
FROM build AS publish

RUN dotnet publish WebApplication.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .

#ENTRYPOINT [“dotnet”, “WebApplication.dll”]

I am of the understanding GemBox.Spreadsheet is Linux-compatible. Unfortunately, I am not very knowledgeable with regard to either Docker containerization or the NGINX web server.

I inherited my current project from another developer and this is my first attempt at utilizing GemBox.Spreadsheet as a potential replacement for EPPlus. While I am very excited about GemBox.Spreadsheet and the ease with which I am able to create Excel worksheets, I need to be able to render PDFs from both IIS and NGINX web servers.

What am I missing here? Any assistance is greatly appreciated.

Hi Randall,

Please read our Docker example.
In short, you are missing fonts.

Regards,
Mario

Hey Mario,

Thanks for the quick reply. I added the lines needed for the “Dockerfile” and, indeed, font packages were retrieved but I am still receiving the same error message:

“This page isn’t working right now. URL can’t currently handle this request. HTTP ERROR 500.”

Thanks again,

Randall Powell

Please send us your sample project so that we can investigate it.

I’m sorry but there is no sample project in play, only an inherited project already in production. I have already provided the germane C# code segment which calls GemBox and, as suggested, I modified the “Dockerfile” script to reflect the following (see italics):

=========================================================
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.3 AS base
WORKDIR /app

#EXPOSE 80
#EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.201 AS build
WORKDIR /src
COPY . WebApplication/
RUN dotnet restore WebApplication/WebApplication.csproj

#COPY . .

WORKDIR /src/WebApplication
RUN dotnet build WebApplication.csproj -c Release -o /app
FROM build AS publish

RUN dotnet publish WebApplication.csproj -c Release -o /app

FROM base AS final

# Update package sources to include supplemental packages (contrib archive area).
RUN sed -i ‘s/main/main contrib/g’ /etc/apt/sources.list

# Downloads the package lists from the repositories.
RUN apt-get update

# Install font configuration.
RUN apt-get install -y fontconfig

# Install Microsoft TrueType core fonts.
RUN apt-get install -y ttf-mscorefonts-installer

# Or install Liberation TrueType fonts.
# RUN apt-get install -y fonts-liberation

# Or some other font package…

WORKDIR /app
COPY --from=publish /app .

#ENTRYPOINT [“dotnet”, “WebApplication.dll”]

==============================================================

In reviewing the output from the executed “Dockerfile” script, there are a number of Linux “debconf” warnings which appear with regard to the added commands. For whatever reason, it does not appear as though the requested font libraries were loaded and at this point I am clueless as to what these “debconf” warnings imply.

I’m truly sorry your product would not work in our Linux-based production environment. I was very excited about GemBox as it performed in our Windows-based development environment and was hoping to expand our usage of it.

Thanks again for your assistance.

Best regards,

Randall Powell

Randall, I’m afraid that I cannot be of much help to you without reproducing your exact issue. That is why I asked for a repro project, you could try creating one by copying your project and then trimming it down significantly (remove anything that is not related to your GemBox usage).

Or perhaps you could do this, download our Docker example on GitHub, add your Excel file to it, and try running it. If it’s successful, try modifying the code and rerunning it. If that is successful as well, then check what is the difference between the two in terms of configuration.

Last, you need to find out the cause of the 500 error, add a global exception handler and log the error that you get.