Coder Social home page Coder Social logo

Comments (23)

simbrams avatar simbrams commented on July 19, 2024

Ok after a Day of research I managed to fix the issue.
First of all, I downloaded all the libraries version (32 & 64 bit) and specified in my .csproj the following:

<ItemGroup> 
      <None Include="GeneratedPdf\**">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>

      <None Include="libs\\32 bit\\libwkhtmltox.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
      <None Include="libs\\32 bit\\libwkhtmltox.dylib">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
      <None Include="libs\\32 bit\\libwkhtmltox.so">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>

      <None Include="libs\\64 bit\\libwkhtmltox.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
      <None Include="libs\\64 bit\\libwkhtmltox.dylib">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
      <None Include="libs\\64 bit\\libwkhtmltox.so">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
  </ItemGroup>

Those lines will make sure that your publish directory will contains all the following files and directories.

After that in you Startup class, please include the following internal class:

internal class CustomAssemblyLoadContext : AssemblyLoadContext
{
     public IntPtr LoadUnmanagedLibrary(string absolutePath)
     {
         return LoadUnmanagedDll(absolutePath);
     }
     protected override IntPtr LoadUnmanagedDll(String unmanagedDllName)
     {
         return LoadUnmanagedDllFromPath(unmanagedDllName);
     }
     protected override Assembly Load(AssemblyName assemblyName)
     {
         throw new NotImplementedException();
     }
}

Then to finish before including your PdfTools as follow:

services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

add the following instead:

CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
var architectureFolder = (IntPtr.Size == 8) ? "64 bit" : "32 bit";
            
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
         var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), $"libs\\{architectureFolder}\\libwkhtmltox.dylib");
          context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
         var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), $"libs\\{architectureFolder}\\libwkhtmltox.so");
         context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
}
else // RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
{
        var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), $"libs\\{architectureFolder}\\libwkhtmltox.dll");
        context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
}
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

It will check your runtime version and your OS in order to load to right Library manually.

You can let Azure default settings (32bit)
This is working on .NET Core 2.2.
I am deploying on Azure through Azure Devops.
Tested on MacOSX and Windows 10 both 64

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

@simbrams Can you submit a PR?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Oh, you had to add the binaries to your main project, not the DinkToPdf package?

from wkhtmltopdf-dotnet.

simbrams avatar simbrams commented on July 19, 2024

Oh, you had to add the binaries to your main project, not the DinkToPdf package?

Exactly, I added them in a subfolder

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Oh I see. That shouldn't be necessary, the goal with the DinkToPdf library is that it's all embedded in the nuget package, just like other packages like Sqlite for example.

from wkhtmltopdf-dotnet.

huysentruitw avatar huysentruitw commented on July 19, 2024

What if we would add wkhtmltopdf as embedded resources and unpack them at application startup? I've seen IronPDF doing this.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

We can try, but I don't think the issue is with the binaries, but with how the sandbox in Azure operates.

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

did a way to use nuget and get these dll's in place ever materialize for an azure deploy?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Unfortunately I haven't had a chance to try it, let's hope for some time over next year!

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I'm running a Web App in Azure, 64-bit .NET Core 3.1 in an S2 App Service Plan and DinkToPdf is working fine. It may not be possible to run in a consumption plan, or Azure function, but in a regular plan it seems to work fine, at least in 64-bit.

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

It works but didnt seem like the huget worked without cooying files by hand can you enumerate your steps?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I didn't have to copy any files manually, I literally just added the NuGet package (Haukcode.DinkToPdf) and deployed to Azure App Services. If you have to copy the binaries then it sounds like you have something else going on. I didn't test with 32-bit though, I've only tested with .NET Core 3.1 64-bit (on Azure, locally I've tested both 32 and 64 bit).

from wkhtmltopdf-dotnet.

PiotrFirst avatar PiotrFirst commented on July 19, 2024

I installed Haukcode.DinkToPdf as HakanL said. Then I still getting error during load project on AZURE.
I commented this line which is responsible for loading lib file: // context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll")); I don't know why but project run on AZURE web service.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Does it work after you uncomment that line (or is it just loading)? What are the specifics about your Azure web service, 32/64 bit, .NET version, consumption plan, etc?

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

@HakanL something is amiss and you've left out some important things.

how did you deploy to azure? using built in VS publish or devops pipeline or kudu build?

also what architecture do you have your project set to.

when i bring in that package and i run a publish to folder i don't see the libwkhtmltox anywhere. i think your test might have been from a dirty state.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I deploy using a pipeline on Visual Studio online (DevOps). 64-bit arch .NET Core 3.1. The deployment is completely automated, not dirty state. Are you sure you're using the Haukcode NuGet package and not the original?

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

yes i'm sure.

ok you answered the missing piece, since you are building on 64 bit you are targeting 64 bit (implicitly, dirty) so dotnet publish will copy them over....as compared to an AnyCPU build which would not.

thx for explaining

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I'm not targeting a particular arch, I just do dotnet publish, so it's framework-dependent. But my Web App (in Azure portal) is configured for 64-bit. If I look in the runtimes folder (on the Azure server) I have the native binaries for all the various architectures (win-x64, win-x86) with the wkhtmltox.dll files in there.
I also just tried to switch it to 32-bit (in the Azure portal) without deployment, and it works. So I know it's not dirty or targeting a particular architecture. FWIW AnyCPU isn't something .NET Core is using, are you targeting the full .NET Framework? I haven't tested that.

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

IIS in Azure will load it in either a 32 or 64-bit process, but all files are included to run in either (in fact even files for linux and mac are included in the package when publishing it, it really is completely non-specific architecture).
Good that you have enough details though!

from wkhtmltopdf-dotnet.

ShahryarSaljoughi avatar ShahryarSaljoughi commented on July 19, 2024

what is the
<None Include="GeneratedPdf\**"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None>

part for?
actually what is GeneratedPdf ?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Those were in his main project, not in DinkToPdf. But that should not be necessary.

from wkhtmltopdf-dotnet.

drdamour avatar drdamour commented on July 19, 2024

That was our workaround before we figured out the targeting was necessary (for our build it was)

from wkhtmltopdf-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.