Creating a pdf of images within Docker

Hello,

I’m trying to create a pdf of images (a list of byte arrays), which works fine in a Windows environment, but I get the following error in my Docker container:

Cannot open an image. The image file format may be not supported at the moment.

  • .NET Core 3.1
  • Debian 10

c# function (relevant):

private static byte[] CreateSnapshotPdf(IEnumerable<byte[]> snapshots)
        {
            var license = new License();
            license.SetLicense("Aspose.PDF.lic");

            var doc = new Document();
            var output = new MemoryStream();

            foreach (var snapshot in snapshots)
            {
                if (snapshot == null)
                {
                    continue;
                }

                var imgStream = new MemoryStream(snapshot);

                var section = doc.Pages.Add();
                section.PageInfo.IsLandscape = true;
                section.PageInfo.Margin.Left = 20;
                section.PageInfo.Margin.Right = 20;
                section.PageInfo.Margin.Top = 20;
                section.PageInfo.Margin.Bottom = 20;

                var image = new Image
                {
                    HorizontalAlignment = HorizontalAlignment.Center
                };

                section.Paragraphs.Add(image);
                image.ImageStream = imgStream;

                image.FixHeight = section.PageInfo.Height - section.PageInfo.Margin.Top - section.PageInfo.Margin.Bottom;
                image.FixWidth = 1000; //section.PageInfo.Width - section.PageInfo.Margin.Left - section.PageInfo.Margin.Right;

                doc.Save(output, SaveFormat.Pdf);
                imgStream.Close();
            }

            return output.ToArray();
        }

Dockerfile (relevant):

# For Aspose PDF - libgdiplus: An Open Source implementation of the GDI+ API.
RUN apt-get update && apt-get install -y libgif-dev autoconf libtool automake build-essential gettext libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
RUN apt-get update && apt-get install -y libgdiplus

@aabril

Could you please also try installing Microsoft Fonts in the environment. In case issue still persists, please share complete exception message with stack trace details with us. We will further proceed to assist you accordingly. Also, please try to test the scenario with 21.2v of the API as well.

Thank you for the response. What would be the best way to install Microsoft Fonts? Do you have example code/command line?

Thanks again

@aabril

You can copy the fonts from Windows folder and paste them in the environment where other fonts are present. OR you can run a command to install the fonts directly:

sudo apt-get install ttf-mscorefonts-installer

It doesn’t look like that package is available. Please see what I get when running your command. Am I missing something?

Reading package lists… Done
Building dependency tree
Reading state information… Done
Package ttf-mscorefonts-installer is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘ttf-mscorefonts-installer’ has no installation candidate

Thank you

I was able to get it to work after installing mscorefonts. For anyone else, here is the relevant part of the Dockerfile. Thank you for your help!

# For Aspose PDF - install libgdiplus + Microsoft fonts
RUN apt-get update && apt-get install -y libgif-dev autoconf libtool automake build-essential gettext libglib2.0-dev libcairo2-dev libtiff-dev libexif-dev
RUN apt-get update && apt-get install -y libgdiplus
RUN echo "# deb http://snapshot.debian.org/archive/debian/20201117T000000Z buster main
deb http://deb.debian.org/debian buster main contrib non-free
# deb http://snapshot.debian.org/archive/debian-security/20201117T000000Z buster/updates main
deb http://security.debian.org/debian-security buster/updates main contrib non-free
# deb http://snapshot.debian.org/archive/debian/20201117T000000Z buster-updates main
deb http://deb.debian.org/debian buster-updates main contrib non-free" | tee /etc/apt/sources.list
RUN apt-get update && apt-get install -y ttf-mscorefonts-installer

@aabril

It is nice to hear that you were able to resolve the issue. Please keep using our API and let us know in case you need further assistance.