I am trying to generate PDFs in asp.net core 3.0 application. Have added the DinkToPdf.dll using nuget package and image added this above 3 files into DinkToPdf folder. Trying to load those DLLs using CustomAssemblyLoadContext.
I am able to generate PDF locally and I generated publish code into one folder. I didn't see the libwkhtmltox.dll in publish code. I added those 3 files manually into publish code and hosted into IIS.
I am facing issue to generate PDF when I host to IIS.
I am using below code:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
var data = Path.Combine(Directory.GetCurrentDirectory(), "DinkToPdf", "libwkhtmltox.dll");
CustomAssemblyLoadContext Context = new CustomAssemblyLoadContext();
Context.LoadUnmanagedLibrary(data);
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
}
}
public 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();
}
}
using below code to generate PDF:
public void GenPdf()
{
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
};
var objectSettings = new ObjectSettings
{
HtmlContent = //html content
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
var file = _converter.Convert(pdf);
}
please help me with this issue.
we have to add above code to load assembly it works for me in iis and kubernet