1

I am trying to use NRecosite HTML to PDF generator.

Whenever it comes to

new NReco.PdfGenerator.HtmlToPdfConverter()

it throws exception

FileNotFoundException: Could not load file or assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

[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);
}
usselite
  • 846
  • 7
  • 24
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • I guess you use 'NReco.PdfGenerator' nuget package which embeds windows wkhtmltopdf binaries and can be used only with .NET Framework. For .NET Core apps 'NReco.PdfGenerator.LT' nuget should be used; it doesn't embed wkhtmltopdf binaries and you need to deploy/install it for your target platform by yourself. LT version is not available for free users (requires a license key). – Vitaliy Fedorchenko Jan 30 '18 at 09:21

1 Answers1

0

It appears that you're missing the link to the Webkit PDF generator 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;

So you need to set the license key (maybe, depends on the version and if you need it licensed) - the code here is for ASP.NET Core. And you have the tag for Core 2.0.

But the tool needs a path defined for the wkhtmltopdf.exe somewhere in your project.

You'll need a copy from here: https://wkhtmltopdf.org/

I've placed mine in the root of the project under \lib\PdfGenerator but place your where suitable for your project.

RemarkLima
  • 11,639
  • 7
  • 37
  • 56
  • You mean I need a exe file to convert html to pdf – maxspan Jan 29 '18 at 12:27
  • Yes, it uses the exe to generate the PDF - https://www.nrecosite.com/doc/NReco.PdfGenerator/html/P_NReco_PdfGenerator_HtmlToPdfConverter_PdfToolPath.htm and you also need a license key for .NET Core. If you're on Unix or Apple OS you'll need the right executable. Also you need the right bit-ness x86 or x64. – RemarkLima Jan 29 '18 at 12:49
  • THERE IS A COMMUNITY EDITION FOR FREE – maxspan Jan 29 '18 at 12:52
  • Not for .NET Core. Free version is only .NET 2.x / 4.x - if you've downloaded via NuGET then you'll need to splash out $75 https://www.nrecosite.com/pdf_generator_net.aspx to get a license key sadly (I'm not part of the project, but use it on a project hence been through it all). If you want free you can use NODE and Phantom JS to generate PDFs (https://stackoverflow.com/questions/39364687/export-html-to-pdf-in-asp-net-core) but I didn't have time to build it out and test...Hence going with NReco – RemarkLima Jan 29 '18 at 12:56
  • I might go with ITextsharp – maxspan Jan 29 '18 at 13:04
  • I can't see anywhere it says it's available (or not!) for .NET Core... When I was looking about a year ago, NReco was the only one setup for Core, so hopefully iTextSharp has been ported as well. – RemarkLima Jan 29 '18 at 13:15