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

2

15

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...

Overcash answered 30/10, 2017 at 17:34 Comment(4)
Possible duplicate of COM library Initilization failed with code 0x80010106 in c#Assimilative
That has nothing to do with my problem.Overcash
Read Hans Passant's comment there. Nothing described in your question has anything to do with your problem except the error code, really.Assimilative
did you ever managed to solve this?Mallett
V
5

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.

Veronaveronese answered 29/11, 2017 at 15:24 Comment(1)
even after inject it still gives the same errorShred
P
1

Set attribute [STAThread] to the CreatePdf method.

You might still get the error on the console, but at least the pdf will be created.

Picked answered 23/10, 2020 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.