rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.08k stars 415 forks source link

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

Open fabercs opened 5 years ago

fabercs commented 5 years ago

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.

fabercs commented 5 years ago

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.

iAmBipinPaul commented 4 years ago

@fabercs I faced same Issue and after replacing all the dll on server with 32 bit it worked for me also.

Thank you :) !

Ksantacr commented 4 years ago

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.

x2

bronzewind commented 4 years ago

@Ksantacr from where did you get the 32 bit one? TY

Ksantacr commented 4 years ago

@Ksantacr from where did you get the 32 bit one? TY

From here https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4/32%20bit

bronzewind commented 4 years ago

Just wanted to check the source... I got it from the same place and I can't make it work, I'm having this error no matter the version. I'm using Core 3.1. Any help would be really much appreciated!

Ksantacr commented 4 years ago

Just wanted to check the source... I got it from the same place and I can't make it work, I'm having this error no matter the version. I'm using Core 3.1. Any help would be really much appreciated!

Full source code https://github.com/rdvojmoc/DinkToPdf/tree/master/src/DinkToPdf I solved this issue in IIS .net core app 3.1

bronzewind commented 4 years ago

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.

Oriahie commented 3 years ago

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.

Same here using core 3.1 ..it works fine locally but doesn't work on iis

jimmytricks commented 3 years ago

Hey guys does anyone have any updates on this? I'm experiencing the same issue. On my local the 64bit library works, however in Azure only the 32bit works. Experiencing the same error as above:

An attempt was made to load a program with an incorrect format.

Does anyone have any thoughts on a way to include both libraries and have the function work out which it requires? Cheers!

IeuanWalker commented 3 years ago

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

jimmytricks commented 3 years ago

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

flotesrx commented 2 years ago

Hi, I have the same issue however my issue is that I can't even make it work locally. I tried replacing 32bit and 64bit but still having this issue. I am using it on my .NET 5 Project and Visual Studio 2019 Community on windows 10 64 bit environment

image

Sach88 commented 1 year ago

I resolved this as below [appreciate everyone's answer which led me to the solution.]

  1. Created Azure Function App [Windows, .Net 6]
  2. Deployed Azure Function [Used 32bit libwkhtmltox.dll]
  3. Got the 'above noted' error.
  4. Tried replacing 64bit "libwkhtmltox.dll' to 32 bit
  5. Still the same error was there.
  6. Then Changed Azure Function app runtime from 32bit to 64bit.
  7. Placed 64bit libwkhtmltox.dll
  8. Did refresh
  9. Tested - Worked fine.

Hope this helps!

Aryesh-M commented 8 months ago

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.<>n0(IDocument document) at DinkToPdf.SynchronizedConverter.<>cDisplayClass5_0.b0() 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, Nullable`1 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.<>n0(IDocument document) at DinkToPdf.SynchronizedConverter.<>cDisplayClass5_0.b0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()<---

Any help on this will be appriciated