TuesPechkin unable to load DLL 'wkhtmltox.dll'
Asked Answered
G

9

15

I've been using TuesPechkin for some time now and today I went to update the nuget package to the new version 2.0.0+ and noticed that Factory.Create() no longer resolved, so I went to read on the GitHub the changes made and noticed it now expects the path to the dll?

IConverter converter =
    new ThreadSafeConverter(
        new PdfToolset(
            new StaticDeployment(DLL_FOLDER_PATH)));

For the past few hours I've tried almost all the paths I can think of, "\bin", "\app_data", "\app_start", etc and I can't seem to find or figure out what it wants for the path and what dll?

I can see the TuesPechkin dll in my bin folder and it was the first path I tried, but I got the following error:

Additional information: Unable to load DLL 'wkhtmltox.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Where is that dll and now can I get it as the library doesn't seem to contain it, I tried installing the TuesPechkin.Wkhtmltox.Win32 package but the dll still is nowhere to be found. Also I am using this in a asp.net website project so I assume that using the following should work for obtaining the path, right?

var path = HttpContext.Current.Server.MapPath(@"~\bin\TuesPechkin.dll");

Further information: https://github.com/tuespetre/TuesPechkin/issues/57

Grabble answered 11/1, 2015 at 0:13 Comment(0)
D
9

The Tuespechkin has a zip file as a resource in the Win32 and Win64 embedded packages for the 'wkhtmltox.dll' file.

What it does when you use the Win32 or Win64 Embedded package is unzips the file and places it in the directory that you specify.

I have been putting a copy of the wkhtmltox dll at the root portion of my web app directory and pointing the DLL_FOLDER_PATH to it using the server physical path of my web app to get to it.

According to the author, you must set the converter in a static field for best results.

I do that, but set the converter to null when I am finished using it, and that seems to work.

Tuespechkin is wrapper for the wmkhtmlox dll file.

The original file is written in C++ and so will not automatically be usable in C# or VB.NET or any of the other managed code domains.

The Tuespechkin.dll file DOES NOT contain a copy of 'wkhtmltox.dll'. You either have to use one of the other embedded deployment modules or install a copy of the 'wkhtmltox.dll' in your web app after downloading it from the internet. That is what I do, and it seems to work just fine.

I am using Team Foundation Server, and attempts to compile code after using the Tuespechkin routines will fail the first time because the 'wkhtmltox.dll' file gets locked, but all you have to do is simply retry your build and it will go through.

I had issues with the 32-bit routine not working in a 64-bit environment and the 64-bit environment not being testable on localhost. I went with the workaround I came up with after examining the source code for Tuespechkin and the Win32 and Win64 embedded deployment packages.

It works well as long as you specify a url for the input rather than raw html.

The older package didn't render css very well.

If you are using a print.aspx routine, you can create the url for it as an offset from your main url.

I don't have the source code I am using with me at this point to offset to your base url for your web application, but it is simply an offshoot of HttpRequest.

You have to use the physical path to find the .dll, but you can use a web path for the print routine.

I hope this answers your question a bit.

Domino answered 11/1, 2015 at 19:53 Comment(2)
I ended up looking through the TuesPechkin repo and inside the Tests project I saw the dll and downloaded it and added it as I said above to the project. I still wonder why did this change? In the older version you didn't need to bother with any DLL folder path, it just worked. :) Anyways I did manage to figure it out but the documentation around the whole DLL folder could be improved. Thank you so much for explaining this further, it was helpful!Grabble
I believe it is done this way because the dll file is not directly referenced anywhere in code. When the file is compiled, the dll file will not necessarily go with it. By including it as a resource file and then unzipping it, the author is ensuring that the codefile will be locatable. Since I really couldn't get the 32-bit embedded deployment module to compile on the TFS server system I was using even with the 32-bit application pool enabled, I used my own workaround.Domino
R
7

If you are getting this error -> Could not load file or assembly 'TuesPechkin.Wkhtmltox.Win64' or one of its dependencies. An attempt was made to load a program with an incorrect format.

In Visual Studio Go to -

Tools -> Options -> Projects and Solutions -> Web Projects -> Use the 64 bit version of IIS Express for web sites and projects.

Ruthie answered 7/7, 2015 at 7:19 Comment(2)
Available only in Visual Studio 2013+Muscolo
The error means you are trying to load the 64-bit version in a 32-bit environment. You have to be set for 64-bit to use the 64-bit version, but then you can't really test it on localhost unless you can also set it to 64-bit.Domino
T
5

I installed TuesPechkin.Wkhtmltox.Win64 Nuget package and used the following code in a singleton:

public class PechkinPDFConvertor : IPDFConvertor
{
    IConverter converter =
               new ThreadSafeConverter(
                  new RemotingToolset<PdfToolset>(
                       new Win64EmbeddedDeployment(
                           new TempFolderDeployment())));

    public byte[] Convert(string html)
    {
        //            return PechkinSync.Convert(new GlobalConfig(), html);
        return converter.Convert(new HtmlToPdfDocument(html));
    }
}

The web application then has to be run in x64 otherwise you will get an error about trying to load an x64 assembly in an x86 environment. Presumably you have to choose x64 or x86 at design time and use the corresponding nuget package, it would be nicer to choose this in the web.config.

EDIT: The above code failed on one server with the exact same message as yours - it was due to having not installed VC++ 2013. So the new code is running x86 as follows

try
{
    string path = Path.Combine(Path.GetTempPath(), "MyApp_PDF_32");
    Converter = new ThreadSafeConverter(
                  new RemotingToolset<PdfToolset>(
                       new Win32EmbeddedDeployment(
                           new StaticDeployment(path))));
}
catch (Exception e)
{
    if (e.Message.StartsWith("Unable to load DLL 'wkhtmltox.dll'"))
    {
        throw new InvalidOperationException(
        "Ensure the prerequisite C++ 2013 Redistributable is installed", e);
    }
    else
        throw;
}
Trichloride answered 19/2, 2015 at 9:11 Comment(6)
Did you figure out a solution that allows you to use the temp directory locally, and then the static deployment in production?Acculturation
@Acculturation No, I haven't managed to spend any more time on this. I'm also keeping an eye on this pull request github.com/tuespetre/TuesPechkin/pull/90Trichloride
I created a version for AnyCPU as well, but it's not what tuespetre is looking for yet. My version just chooses the wkhtmltopdf DLL based on the current system architecture. It works for me, but he wants an AnyCPU library that references both the 32-bit and 64-bit libraries he has created. I assume this is so that the DLL files for wkhtmltopdf aren't uploaded in more than one project, which would make it easier to update. Makes sense. I'm working to create a new solution that will satisfy this criteria when I get time. Thanks for the reply.Acculturation
Installing the Visual C++ Redistributable for Visual Studio 2013 resolved the error for me. microsoft.com/en-us/download/details.aspx?id=40784Pena
I also resolved this by installing Visual C++ Redistributable for Visual Studio 2013 !Shorthanded
Worth noting, I had to install the x86 version for it to work even though it's a x64 server.Expunction
B
2

If you do not want run the installer for wkhtmltox just to get the dll, you can do the following:

As @Timothy suggests, if you use the embedded version of wkhtmltox.dll from TuesPechkin, it will unzip it and place it in a temp directory. I copied this dll and referenced it with the StaticDeployment option without any issues.

To find the exact location, I just used Process Monitor (procmon.exe). For me it was C:\Windows\Temp\-169958574\8\0.12.2.1\wkhtmltox.dll

Beekman answered 9/9, 2015 at 15:28 Comment(1)
Having copied the appropriate DLL unpacked via the TempFolderDeployment call to the web project's bin folder, the answer to the original question--what is the necessary path for the StaticDepolyment c'tor?--seems to be Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")Overall
R
1

In my case, I am deploying on a 64-bit VPS then I got this error. I have solved the problem by installing the wkhtmltopdf that I downloaded from http://wkhtmltopdf.org/downloads.html. I chose the 32-bit installer.

Rockribbed answered 3/2, 2015 at 6:3 Comment(0)
L
1

In my case, I have solved the problem by installing the Wkhtmltox for win32 at https://www.nuget.org/packages/TuesPechkin.Wkhtmltox.Win32/

Leaper answered 26/6, 2015 at 4:22 Comment(0)
L
1

This error: Unable to load DLL 'wkhtmltox.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) is returned in two situations:

1- Deploy dependency not installed:

For solve this, you can install nuget package "TuesPechkin.Wkhtmltox.Win64" and use this code (for WebApplications running in IIS):

IConverter converter =
    new ThreadSafeConverter(
        new RemotingToolset<PdfToolset>(
            new Win64EmbeddedDeployment(
                new TempFolderDeployment())));

// Keep the converter somewhere static, or as a singleton instance!
// Do NOT run the above code more than once in the application lifecycle!

byte[] result = converter.Convert(document);

In runtime this code will copy the dependency "wkhtmltox.dll" in a temporary directory like: "C:\Windows\Temp\1402166677\8\0.12.2.1". It's possible to get the destination of file using:

var deployment = new Win64EmbeddedDeployment(new TempFolderDeployment());
Console.WriteLine(deployment.Path);

2- Microsoft Visual C++ 2013 Redistributable not installed:

As described here: https://github.com/tuespetre/TuesPechkin/issues/65#issuecomment-71266114, the Visual C++ 2013 Runtime is required.

The solution from README is:

You must have Visual C++ 2013 runtime installed to use these packages. Otherwise, you will need to download the MingW build of wkhtmltopdf and its dependencies from their website and use that with the library. https://github.com/tuespetre/TuesPechkin#wkhtmltoxdll

or, you can install the Microsoft Visual C++ 2013 Redistributable:

choco install msvisualcplusplus2013-redist
Lifeanddeath answered 19/5, 2020 at 18:38 Comment(1)
it's a working advice ! Installing TuesPechkin.Wkhtmltox.Win64 in order to install the Deploy dependency was what to do for me (IIS)Valiant
A
1

Installing the Visual C++ Redistributable for Visual Studio 2013 resolved the error for me.

https://www.microsoft.com/en-us/download/details.aspx?id=40784

Allanallana answered 20/12, 2020 at 9:2 Comment(0)
M
0

Here is AnyCpu version, also support iis-base or winform application

using TuesPechkin.Wkhtmltox.AnyCPU;
...
var converter = PDFHelper.Factory.GetConverter();
var result = converter.Convert(This.Document);

Reference : https://github.com/tloy1966/TuesPechkin

Major answered 11/11, 2019 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.