1

我正在尝试使用 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 上运行该项目,那就完美了!

先感谢您!

4

2 回答 2

0

就我而言:net core 2.1 版和 /opt/projects/.. 上的主要应用程序位置

  1. 我将 libwkhtmltox.so 移动到 /usr/lib
  2. 须藤 ldconfig
  3. sudo ldconfig -v | grep libwk -> (libwkhtmltox.so.0 -> libwkhtmltox.so)

然后下载wkhtmltopdf

  1. 安装所有依赖 wkhtmltopdf(取决于版本)
  2. sudo dpkg -i wkhtmltox_version.***.deb
  3. 键入 wkhtmltopdf --version (只需确保已安装)
  4. 测试 wkhtmltopdf http://google.com google.pdf

重新运行您的应用程序

于 2021-08-13T07:40:05.930 回答
0

尝试运行此命令apt-get install -y --no-install-recommends zlib1g fontconfig libfreetype6 libx11-6 libxext6 libxrender1。它对我有用!

于 2020-04-30T05:30:31.607 回答