Dotnet Core:在 Linux 上使用 PDFium 将 Pdf 转换为图像获取 DllNotFoundException

问题描述

我尝试使用 pdfium 在 linux 中将 pdf 转换为图像。但得到这个错误:

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

这是我的源代码:

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,true);
                bitmapImage.Save(@"Test.jpg",ImageFormat.Jpeg);
            }
        }
    }
}

这是 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>

这里是 docker-compose 文件:

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"

可以使用以下命令运行程序: docker-compose run --rm app

我尝试了其他库,如“PDFium.LinuxV2”或“PDFium.Linux.x64”而不是“PDFiumCore”,但没有做出任何改变。

解决方法

我错误地认为 PdfiumCore 是 PdfiumViewer 的二进制提供程序包。但它是一个独立的包,因为它使用更新的 Pdfium 版本(这似乎是使用 pdfium 的一个关键点)我决定使用它。 (我还测试了 https://github.com/GowenGit/docnet,它运行良好,但它使用的是旧版本的 PDFium)

因此使用 https://github.com/Dtronix/DtronixPdf/(这是 PdfiumCore 的线程安全实现)并对其进行清理以从中制作 PDF To Image Converter,并在 Windows 和 Linux 上对其进行测试。 这是最终代码:https://github.com/hmdhasani/DtronixPdf/blob/master/src/DtronixPdfBenchmark/Program.cs

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...