Search code examples
linux.net-corepdfium

Dotnet Core: use PDFium to convert Pdf to Image on Linux get DllNotFoundException


I try to use pdfium to convert pdf to image in linux. but got this error:

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'pdfium.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libpdfium.dll: cannot open shared object file: No such file or directory
   at PdfiumViewer.NativeMethods.Imports.FPDF_AddRef()
   at PdfiumViewer.NativeMethods.FPDF_AddRef()
   at PdfiumViewer.PdfLibrary..ctor()
   at PdfiumViewer.PdfLibrary.EnsureLoaded()
   at PdfiumViewer.PdfFile..ctor(Stream stream, String password)
   at PdfiumViewer.PdfDocument..ctor(Stream stream, String password)
   at PdfiumViewer.PdfDocument.Load(Stream stream, String password)
   at PdfiumViewer.PdfDocument.Load(String path, String password)
   at PdfiumViewer.PdfDocument.Load(String path)
   at PDFiumOnLinux.Program.Main(String[] args) in /src/Program.cs:line 10

This is my source code:

using PdfiumViewer;
using System.Drawing.Imaging;

namespace PDFiumOnLinux
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var pdfDocument = PdfDocument.Load(@"Test.pdf"))
            {
                var bitmapImage = pdfDocument.Render(0, 300, 300, true);
                bitmapImage.Save(@"Test.jpg", ImageFormat.Jpeg);
            }
        }
    }
}

This is the csporj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PdfiumViewer" Version="2.13.0" />
    <PackageReference Include="PDFiumCore" Version="4503.0.0" />
    <PackageReference Include="System.Drawing.Common" Version="5.0.2" />
  </ItemGroup>

</Project>

and here's the docker-compose file:

version: "3.9"
services:
    app:
        image: mcr.microsoft.com/dotnet/sdk:3.1.409-buster
        volumes:
            - .:/src
        working_dir: /src
        entrypoint: bash -c "dotnet build PDFiumOnLinux.csproj && dotnet bin/Debug/netcoreapp3.1/PDFiumOnLinux.dll"

The program can be run using this command: docker-compose run --rm app

I tried other libraries like 'PDFium.LinuxV2' or 'PDFium.Linux.x64' instead of 'PDFiumCore' but made no change.


Solution

  • I was wrong thinking PdfiumCore is a binary provider package for PdfiumViewer. but it's an standalone package and as it uses a much newer Pdfium version (that seems to be a critical point for using pdfium) I decided to use it. (I also tested https://github.com/GowenGit/docnet and it worked fine but it use an older version of PDFium)

    So used https://github.com/Dtronix/DtronixPdf/ (that is a thread safe implementation of PdfiumCore) and cleaned it to make a PDF To Image Converter from it and tested it on Windows and Linux. here's the final code: https://github.com/hmdhasani/DtronixPdf/blob/master/src/DtronixPdfBenchmark/Program.cs