DinkToPdf: System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

I have asp.net core 2.1 project which I use 64 bit libwkhtmltox.dll. Locally all works fine either while debugging or hosting on local iis. However when I host it on server which is win 64 bit OS, exception is thrown as like;

System.AggregateException: One or more errors occurred. (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)) ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics)
   at DinkToPdf.PdfTools.Load()
   at DinkToPdf.BasicConverter.Convert(IDocument document)
   at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document)
   at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__278_1(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   --- End of inner exception stack trace ---
   at DinkToPdf.SynchronizedConverter.Invoke[TResult](Func`1 delegate)
   at DinkToPdf.SynchronizedConverter.Convert(IDocument document)

deps.json configuration is as follows

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v2.1",
    "signature": "17c0d97ba89af48f20c96c6f42ea9f22e1c7de55"
  },
  "compilationOptions": {
    "defines": [
      "TRACE",
      "RELEASE",
      "NETCOREAPP",
      "NETCOREAPP2_1"
    ],
    "languageVersion": "",
    "platform": "AnyCPU",
    "allowUnsafe": false,
    "warningsAsErrors": false,
    "optimize": true,
    "keyFile": "",
    "emitEntryPoint": true,
    "xmlDoc": false,
    "debugType": "portable"
  },
  "targets": {
    ".NETCoreApp,Version=v2.1": {
      "KatefWeb/1.0.0": {
        "dependencies": {
          "Dapper": "1.50.5",
          "Dapper.Contrib": "1.50.5",
          "DinkToPdf": "1.0.8",
          "Katef.Core": "1.0.0",
          "Microsoft.AspNetCore.App": "2.1.1",
          "Microsoft.NETCore.App": "2.1.0",
          "Microsoft.VisualStudio.Web.CodeGeneration.Design": "2.1.1",
          "Serilog": "2.8.0",
          "Serilog.AspNetCore": "2.1.1",
          "Serilog.Settings.Configuration": "3.0.1",
          "Serilog.Sinks.MSSqlServer": "5.1.2",
          "System.Drawing.Common": "4.5.1"
        },

Publishing with options Portable and Framework-Dependent.

About this issue

Most upvoted comments

As I know this exception is a 64 bit/ 32 bit conflict in general, I replaced the dll on server with a 32 bit one and exception gone, but I don’t know why.

Could you share an example project? It does not work for me at all. I have even created an independent issue. No matter what I try it just doesn’t work and the error is the same.

Hello I tried updating Azure to 64 bit but still giving the error

System.AggregateException: One or more errors occurred. —> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics) at DinkToPdf.PdfTools.Load() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\PdfTools.cs:line 27 at DinkToPdf.BasicConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\BasicConverter.cs:line 42 at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document) at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- at DinkToPdf.SynchronizedConverter.Invoke[TResult](Func1 delegate) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 48 at DinkToPdf.SynchronizedConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at PdfService.GeneratePdf(String header, String htmlContent) in D:\Yash\Transom Corporation\Scentia Selection Program\selection-program\api\WebApi\Helpers\PDF\PdfService.cs:line 19 at WebApi.Controllers.PdfController.Get(String id, String type, Nullable1 dimensions, Nullable1 electrical, String funds, Nullable1 multiplier, String delivery, String terms) in D:\Yash\Transom Corporation\Scentia Selection Program\selection-program\api\WebApi\Controllers\PdfController.cs:line 266 ---> (Inner Exception #0) System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics) at DinkToPdf.PdfTools.Load() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\PdfTools.cs:line 27 at DinkToPdf.BasicConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\BasicConverter.cs:line 42 at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document) at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.Tasks.Task.Execute()<—

Any help on this will be appriciated

Thanks @IeuanWalker - haven’t tried this yet, but looks like it’ll resolve it, cheers

@jimmytricks you can change azure to be 64bit - image