如何解决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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。