【发布时间】:2019-08-05 15:40:32
【问题描述】:
我正在尝试使用 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 上运行该项目,那就完美了!
提前谢谢你!
【问题讨论】:
-
你检查
assemblyPath的值是多少? Mb 它指向错误的目录 -
是的,检查过,这是我项目的根目录,顺便说一句,我也试过不带“/”
-
您是否安装了
libgdiplus?sudo apt install libgdiplus -
是的,已安装。 sudo apt-get install libgdiplus
-
我也面临同样的问题。
标签: c# html pdf asp.net-core-webapi dinktopdf