1

I use NReco HtmlToPdfConverter and usually it works correctly. But sometimes I face with the following error:

Cannot generate PDF: The specified executable is not a valid application for this OS platform.

The code is quite simple, it is a singletone class:

public class Converter : IConverter
{
    private readonly object lockObject = new object();

    public Converter()
    {
        HtmlToPdf = new HtmlToPdfConverter();
    }

    private HtmlToPdfConverter HtmlToPdf { get; }

    public byte[] GeneratePdf(string htmlContent)
    {
        lock (lockObject)
        {
            return HtmlToPdf.GeneratePdf(htmlContent);
        }
    }
}

Does anybody know what can be a reason for such kind of error?

Ilya Shpakovsky
  • 281
  • 1
  • 3
  • 16

1 Answers1

0

If you use NReco.PdfGenerator nuget package which embeds wkhtmltopdf binaries this might be possible if you use HtmlToPdfConverter class simultaneously from different threads. Try to do the following:

  • ensure that the same instance of HtmlToPdfConverter class is not used from different threads (simultaneously). Each thread should have its own instance of HtmlToPdfConverter class, and as result they will use different wkhtmltopdf processes.
  • wkhtmltopdf binaries are extracted on first use. You may force this on the application start (by calling "GeneratePdf" to perform some 'fake' conversion) to avoid possible problems with files extraction later. Or, you can switch to NReco.PdfGenerator.LT nuget package - this is wrapper-only - and deploy wkhtmltopdf by yourself (no need to extract binaries = executable cannot be corrupted); note that this nuget package can be used only by commercial users.
Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • yes, I generate pdf from differend threads and use multiple servers. But I use lock statement in order to synchronize it within one machine. I will try to call "GeneratePdf" on app start – Ilya Shpakovsky Dec 10 '18 at 09:28