【问题标题】:Convert HTML to PDF in asp.net core using DinkToPdf is not working in Ubuntu使用 DinkToPdf 在 asp.net 核心中将 HTML 转换为 PDF 在 Ubuntu 中不起作用
【发布时间】: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 它指向错误的目录
  • 是的,检查过,这是我项目的根目录,顺便说一句,我也试过不带“/”
  • 您是否安装了libgdiplussudo apt install libgdiplus
  • 是的,已安装。 sudo apt-get install libgdiplus
  • 我也面临同样的问题。

标签: c# html pdf asp.net-core-webapi dinktopdf


【解决方案1】:

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

  1. 我将 libwkhtmltox.so 移动到 /usr/lib
  2. sudo 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.comgoogle.pdf

重新运行您的应用程序

【讨论】:

  • 在我的启动文件中我没有使用 CustomAssemblyLoader。我只是添加为服务-> services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
【解决方案2】:

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

【讨论】:

  • 请编辑您的答案以使其更具解释性。例如,要在哪里运行此命令?,需要采取哪些必要步骤,这些将帮助任何想要利用您的答案的人。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多