我正在尝试使用 ASP.NET Core Web API 2.1 版中的 DinkToPdf 将 HTML 文档转换为 PDF。
这是我的方法:
public async Task<ActionResult> ToPdf()
{
var loader = new CustomAssemblyLoader();
var assemblyPath = Directory.GetCurrentDirectory();
loader.LoadUnmanagedLibrary($@"{assemblyPath}/libwkhtmltox");
var doc = new HtmlToPdfDocument()
{
GlobalSettings = {
Orientation = Orientation.Landscape,
PaperSize = PaperKind.A4,
Out = "wwwroot/test.pdf"
},
Objects = {
new ObjectSettings() {
PagesCount = true,
HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices iaculis. Utodio viverra, molestie lectus nec, venenatis turpis.",
WebSettings = { DefaultEncoding = "utf-8" },
HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
}
}
};
_pdfConverter.Convert(doc);
}
我将 libwkhtmltox.so 和 libwkhtmltox.dll 复制到我的项目的根目录,每个 .dll 都位于该根目录。
sudo apt-get install libgdiplus
总是得到这个错误
DllNotFoundException: Unable to load shared library '/var/www/webapi/webapi/libwkhtmltox' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /var/www/webapi/webapi/libwkhtmltox: cannot open shared object file: No such file or directory
顺便说一句,如果我在 Windows 上运行该项目,那就完美了!
先感谢您!