【问题标题】:Can not find compilation library location for package 'NReco.PdfGenerator'找不到包“NReco.PdfGenerator”的编译库位置
【发布时间】:2018-01-29 06:29:38
【问题描述】:

我正在尝试使用 NRecosite HTML to PDF 生成器。

无论何时

new NReco.PdfGenerator.HtmlToPdfConverter()

抛出异常

FileNotFoundException: 无法加载文件或程序集 'System.Web, 版本=2.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。 系统找不到指定的文件。

[HttpPost]
public async Task<IActionResult> Login(RegistrationModel model, CommandType command)
{
    int selectedTab = 9;
    if (command == CommandType.SignApplication)
    {

        selectedTab = 10;
        string htmlContent = await GetHtml(model.Review.PublishUrl);
        var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
        var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
        return File(pdfBytes, "application/pdf", "dd.pdf");
    }

    RegistrationManager mgr = new RegistrationManager(_registrationRepository);
    var registrationModel = await mgr.CreateLoginModel(model.FormId, model.LeadId, selectedTab);
    return registrationModel.IsRegistrationDisbaled ? View("Error", "The link is disabled for this account") : View("Index", registrationModel);
}

【问题讨论】:

  • 我猜你使用'NReco.PdfGenerator' nuget 包,它嵌入了windows wkhtmltopdf 二进制文件并且只能与.NET Framework 一起使用。对于 .NET Core 应用程序,应使用“NReco.PdfGenerator.LT”nuget;它不嵌入 wkhtmltopdf 二进制文件,您需要自己为您的目标平台部署/安装它。 LT 版本不适用于免费用户(需要许可证密钥)。

标签: c# asp.net-core-2.0 nreco


【解决方案1】:

您似乎缺少指向 Webkit PDF 生成器 exe 的链接。

// Let's add the recovery demand to the right folder
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();

htmlToPdf.License.SetLicenseKey("genertor","Your license key");

htmlToPdf.PdfToolPath = _env.ContentRootPath + "\\lib\\PdfGenerator";;
htmlToPdf.TempFilesPath = _env.ContentRootPath + "\\lib\\PdfGenerator";;

htmlToPdf.Size = NReco.PdfGenerator.PageSize.A4;
htmlToPdf.Orientation = NReco.PdfGenerator.PageOrientation.Portrait;
htmlToPdf.LowQuality = false;

所以您需要设置许可密钥(可能取决于版本以及是否需要许可)——此处的代码适用于 ASP.NET Core。而且你有 Core 2.0 的标签。

但该工具需要为您项目中某处的wkhtmltopdf.exe 定义路径。

您需要从这里获得一份副本:https://wkhtmltopdf.org/

我已将我的放在项目的根目录中 \lib\PdfGenerator 下,但将您放在适合您项目的位置。

【讨论】:

  • 你的意思是我需要一个exe文件来将html转换为pdf
  • 是的,它使用 exe 生成 PDF - nrecosite.com/doc/NReco.PdfGenerator/html/…,您还需要 .NET Core 的许可证密钥。如果您使用的是 Unix 或 Apple OS,您将需要正确的可执行文件。您还需要正确的 x86 或 x64 位。
  • 有一个免费的社区版
  • 不适用于 .NET Core。免费版本只有 .NET 2.x / 4.x - 如果你是通过 NuGET 下载的,那么你需要花 75 美元 nrecosite.com/pdf_generator_net.aspx 才能获得许可证密钥(我不是项目的一部分,但是在项目中使用它,因此经历了这一切)。如果你想免费,你可以使用 NODE 和 Phantom JS 来生成 PDF (stackoverflow.com/questions/39364687/…) 但我没有时间构建它并测试...因此选择 NReco
  • 我可能会选择 ITextsharp
猜你喜欢
  • 2017-05-04
  • 2022-01-11
  • 1970-01-01
  • 2018-12-18
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多