Comments (23)
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.
@simbrams Can you submit a PR?
from wkhtmltopdf-dotnet.
Oh, you had to add the binaries to your main project, not the DinkToPdf package?
from wkhtmltopdf-dotnet.
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.
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.
What if we would add wkhtmltopdf as embedded resources and unpack them at application startup? I've seen IronPDF doing this.
from wkhtmltopdf-dotnet.
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.
did a way to use nuget and get these dll's in place ever materialize for an azure deploy?
from wkhtmltopdf-dotnet.
Unfortunately I haven't had a chance to try it, let's hope for some time over next year!
from wkhtmltopdf-dotnet.
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.
It works but didnt seem like the huget worked without cooying files by hand can you enumerate your steps?
from wkhtmltopdf-dotnet.
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.
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.
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.
@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.
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.
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.
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.
from wkhtmltopdf-dotnet.
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.
what is the
<None Include="GeneratedPdf\**"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None>
part for?
actually what is GeneratedPdf
?
from wkhtmltopdf-dotnet.
Those were in his main project, not in DinkToPdf. But that should not be necessary.
from wkhtmltopdf-dotnet.
That was our workaround before we figured out the targeting was necessary (for our build it was)
from wkhtmltopdf-dotnet.
Related Issues (20)
- Execution time increases under load HOT 1
- I didn't use dependency injection. Using static creation would cause the thread to get stuck and new requests to keep getting stuck HOT 22
- % in the href attribute are still encoded to %25 HOT 2
- Not reading settings on alpine HOT 2
- HTML tags removed HOT 3
- Reporting a vulnerability HOT 1
- In Linux CentOS 7, there was an error: Unable to load native library. The platform may be missing native dependencies (libjpeg62, etc). Or the current platform is not supported. HOT 3
- Memory overflow HOT 2
- The wkhtmltopdf project that is used, is no longer maintained HOT 3
- Display flex is not working HOT 1
- Unable to load native library - Windows 10/11 HOT 16
- How to directly pass parameters to wkhtmltopdf without using objects HOT 1
- You can only download the pdf once HOT 2
- How can I include CSS like Bootstrap in my HTML so that I can observe the applied styles when generating a PDF? HOT 6
- Unable to load native library on .NET8 HOT 29
- n/a
- GlobalSettings not respected on debian 11 HOT 3
- With Multiple requests fails HOT 15
- Not working on Azure Web App 32 bits HOT 7
- add cover command HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wkhtmltopdf-dotnet.