[英]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.我尝试使用 pdfium 将 pdf 转换为 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:这是 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:这是 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"
The program can be run using this command: docker-compose run --rm app
该程序可以使用以下命令运行: docker-compose run --rm app
I tried other libraries like 'PDFium.LinuxV2' or 'PDFium.Linux.x64' instead of 'PDFiumCore' but made no change.我尝试了其他库,例如“PDFium.LinuxV2”或“PDFium.Linux.x64”而不是“PDFiumCore”,但没有进行任何更改。
I was wrong thinking PdfiumCore is a binary provider package for PdfiumViewer.我错误地认为 PdfiumCore 是 PdfiumViewer 的二进制提供程序 package。 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.但它是一个独立的 package 并且由于它使用更新的 Pdfium 版本(这似乎是使用 pdfium 的关键点),我决定使用它。 (I also tested https://github.com/GowenGit/docnet and it worked fine but it use an older version of PDFium) (我还测试了 https://github.com/GowenGit/docnet ,它运行良好,但它使用的是旧版本的 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. 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这是最终代码: https://github.com/hmdhasani/DtronixPdf/blob/master/src/DtronixPdfBenchmark/Program.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.