简体   繁体   中英

Not able to generate PDF in IIS using DinkToPDF and working locally

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.

I'm having issues to use DinkToPdf also in.Net Core 3.1. I have tried both versions 32bit & 64bit and no luck. Did you have any progress?

services.AddControllersWithViews();
            var architectureFolder = (IntPtr.Size == 8) ? "64 bit" : "32 bit";
            var wkHtmlToPdfPath = Path.Combine(_hostingEnvironment.ContentRootPath, $"wkhtmltox\\v0.12.4\\{architectureFolder}\\libwkhtmltox");
            CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(wkHtmlToPdfPath);

we have to add above code to load assembly it works for me in iis and kubernet

https://www.nuget.org/packages/Haukcode.WkHtmlToPdfDotNet/ worked for me, when upgrading from netcoreapp2.1 to netcoreapp3.1 .

replace your refs to DinkToPdf to:

using WkHtmlToPdfDotNet;
using WkHtmlToPdfDotNet.Contracts;

I used Haukcode.WkHtmlToPdfDotNet version 1.4.0 and my libwkhtmltopdf was 0.12.6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM