How to Work with PDF Files in .Net Core on Docker and Linux

IronPDF now fully supports Docker, Linux and MacOS deployments and debugging. This allows true cross platform portability of our C# PDF library. 
Now we can Create, Edit and Read PDFs on Linux, Mac and Docker with IronPDF

Prerequisites

When we deploy to Linux and Docker we have to ensure that the instance has the relevant packages installed to support PDF rendering.

These packages are often not present in minimalist Linux builds, Docker VMS - but are easy to add.

Working with Docker

First we recommend this excellent article on setting up Docker debugging and integration with Visual Studio projects. https://docs.microsoft.com/en-us/visualstudio/containers/edit-and-refresh?view=vs-2019

Cleaning Up First

Amend your Docker File

Adding these RUN ["apt-get"] lines to your Dockerfile. Generally at the top, normally second line just after the first docker image is defined

# FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
# After first docker image is defined - install all these dependancies

RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "libgdiplus"]
RUN ["apt-get", "-y", "install", "xvfb", "libfontconfig", "wkhtmltopdf"]
RUN ["apt-get", "-y", "install", "libc6-dev"]
RUN ["apt-get", "-y", "install", "openssl"]
RUN ["apt-get", "-y", "install", "libssl1.0-dev"]

Alternative Docker IronPDF Setup Solution in C# Code

If you find Docker files frustrating (see above); instead of amending the Docker file we can programmatically require the Linux / Docker dependancies for the PDF library.

Add the following code to your solution and run it only once per dployment - this will take a few minutes the next time you run IronPDF. Watch the Visual Studio "Output" window for progress.

 if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
    {
        Debug.Write("Attempting to automatically install Linux / Docker dependencies.  This may take some time on your first run on this machine.");

        try
        {
            var p1 = System.Diagnostics.Process.Start("apt-get", "update");
            p1.WaitForExit();
            var p2 = System.Diagnostics.Process.Start("apt-get", " install -y libgdiplus");
            p2.WaitForExit();
            var p3 = System.Diagnostics.Process.Start("apt-get", "install -y   xvfb libfontconfig wkhtmltopdf");
            p3.WaitForExit();
            var p4 = System.Diagnostics.Process.Start("apt-get", " install -y   libc6-dev");
            p4.WaitForExit();
            var p5 = System.Diagnostics.Process.Start("apt-get", " install -y   openssl");
            p5.WaitForExit();
            var p6 = System.Diagnostics.Process.Start("apt-get", " install -y   libssl1.0-dev");
            p6.WaitForExit();

            Debug.Write("Linux / Docker dependency installation success");

            var msg = e.Message;
        }catch{
            Debug.Write("Linux / Docker dependency installation failure");
        }
    }
If System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux) Then
		Debug.Write("Attempting to automatically install Linux / Docker dependencies.  This may take some time on your first run on this machine.")

		Try
			Dim p1 = System.Diagnostics.Process.Start("apt-get", "update")
			p1.WaitForExit()
			Dim p2 = System.Diagnostics.Process.Start("apt-get", " install -y libgdiplus")
			p2.WaitForExit()
			Dim p3 = System.Diagnostics.Process.Start("apt-get", "install -y   xvfb libfontconfig wkhtmltopdf")
			p3.WaitForExit()
			Dim p4 = System.Diagnostics.Process.Start("apt-get", " install -y   libc6-dev")
			p4.WaitForExit()
			Dim p5 = System.Diagnostics.Process.Start("apt-get", " install -y   openssl")
			p5.WaitForExit()
			Dim p6 = System.Diagnostics.Process.Start("apt-get", " install -y   libssl1.0-dev")
			p6.WaitForExit()

			Debug.Write("Linux / Docker dependency installation success")

			Dim msg = e.Message
		Catch
			Debug.Write("Linux / Docker dependency installation failure")
		End Try
End If
VB   C#

Linux Support

We just need to make sure the dependencies are there. Its as easy as

apt-get update
apt-get install -y libgdiplus xvfb libfontconfig wkhtmltopdf libc6-dev openssl libssl1.0-dev

the same can be achieved

MacOS Support

We have found that mostly OS X systems already have the needed dependancies. The same packages as for linux can be installed using HomeBrew.