Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Could not initialize OLE (error 80010106) - (libwkhtmltox.dll) on C#

im using this libwkhtmltox.dll to convert my html to pdf. Im using c# .netCore.

Usage:

    private static string CreatePdf(string content, string fileName)
    {
        var fullPath = $"{_projectSettingsOptions.Pdf}/{fileName}";

        using (var sw = new StreamWriter(new FileStream(fullPath, FileMode.Create), Encoding.UTF8))
        {
            sw.Write(content);
            sw.Flush();
        }

        new Pdf(_projectSettingsOptions).Convert(fullPath, content);

        return fullPath;
    }

About the code above:

  • content = The html content i want to convert to pdf.
  • fullPath = Where the html content will be stored.
  • _projectSettingsOptions = Paths of the project, like image paths, pdf paths and so on...

Then i call the Pdf class, passing the path and the content to convert.

new Pdf(_projectSettingsOptions).Convert(fullPath, content);

This is my Pdf class:

namespace SiteMFPManager.Library.Util
{

using Assembly;
using DinkToPdf;
using Settings;
using System.IO;

public class Pdf
{
    public Pdf(ProjectSettingsOptions projectSettingsOptions) =>
        new CustomAssemblyLoadContext().LoadUnmanagedLibrary(Path.Combine(projectSettingsOptions.References, "libwkhtmltox", "libwkhtmltox.dll"));

    public void Convert(string fullPath, string content) =>
        new SynchronizedConverter(new PdfTools()).Convert(new HtmlToPdfDocument
        {
            GlobalSettings =
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 10 },
                Out = fullPath,
            },
            Objects =
            {
                new ObjectSettings
                {
                    PagesCount = true,
                    HtmlContent = content,
                    WebSettings = { DefaultEncoding = "utf-8" }
                }
            }
        });
    }
}

The CreatePdf method its executed two times. In the first time, it executes and show to me in the console, this: Qt: Could not initialize OLE (error 80010106), when the code is executed for the second time, the application stops, no exceptions happen, nothing... Just that message.

If you need more information to help me with this problem, just tell me.

Sorry if the post is bad formatted, im new to this...

like image 573
Victor Hugo Schmidt Avatar asked Oct 30 '17 17:10

Victor Hugo Schmidt


1 Answers

To get the SynchronizedConverter working properly in .Net Core you need to register it as a Singleton (Most likely in Startup.cs ConfigureServices):

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

(Also described here: https://github.com/rdvojmoc/DinkToPdf#dependancy-injection)

After that you can inject the Converter into your Controller instead of creating a new instance. This way the DLL gets called always in the same Thread.

like image 68
Yush0 Avatar answered Nov 08 '22 04:11

Yush0