DinkToPdf: Qt: Could not initialize OLE (error 80010106)

I’m working on a ASP.NET Core MVC app and I wanted to use DinkToPdf library to convert Html to PDF. I get this error in the console, when I first access a page that uses the IConverter from the services:

Qt: Could not initialize OLE (error 80010106)

I still get the PDF with no issues. I think the issue is somewhere around calling: new SynchronizedConverter(new PdfTools()) As I have it in the services as a singleton and only called the first time it’s accessed:

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

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 5
  • Comments: 22

Commits related to this issue

Most upvoted comments

I’ve just hit this problem too, in an Azure Isolated Functions host project.

I’m pretty sure this issue is due to the worker thread in SynchronizedConverter not being STA mode. So I copied the SynchronizedConverter and made an STA version.

public class STASynchronizedConverter : BasicConverter
{
    Thread conversionThread;

    BlockingCollection<Task> conversions = new BlockingCollection<Task>();

    bool kill = false;

    private readonly object startLock = new object();

    public STASynchronizedConverter(ITools tools) : base(tools)
    {
    }

    public override byte[] Convert(IDocument document)
    {
        return Invoke(() => base.Convert(document));
    }

    public TResult Invoke<TResult>(Func<TResult> @delegate)
    {
        StartThread();

        Task<TResult> task = new Task<TResult>(@delegate);

        lock (task)
        {
            //add task to blocking collection
            conversions.Add(task);

            //wait for task to be processed by conversion thread
            Monitor.Wait(task);
        }

        //throw exception that happened during conversion
        if (task.Exception != null)
        {
            throw task.Exception;
        }

        return task.Result;
    }

    private void StartThread()
    {
        lock (startLock)
        {
            if (conversionThread == null)
            {
                conversionThread = new Thread(Run)
                {
                    IsBackground = true,
                    Name = "wkhtmltopdf worker thread"
                };

                // This is to fix issue https://github.com/rdvojmoc/DinkToPdf/issues/119
                conversionThread.SetApartmentState(ApartmentState.STA);

                kill = false;

                conversionThread.Start();
            }
        }
    }

    private void StopThread()
    {
        lock (startLock)
        {
            if (conversionThread != null)
            {
                kill = true;

                while (conversionThread.ThreadState == ThreadState.Stopped)
                { }

                conversionThread = null;
            }
        }
    }

    private void Run()
    {
        while (!kill)
        {
            //get next conversion taks from blocking collection
            Task task = conversions.Take();

            lock (task)
            {
                //run taks on thread that called RunSynchronously method
                task.RunSynchronously();

                //notify caller thread that task is completed
                Monitor.Pulse(task);
            }
        }
    }
}

Thanks, @distantcam it is work.

only notice for all, To access the Converter class, you must inject it, not create a new instance.

public YourService(IIConverter converter) 
{
        _converter = converter;
}

HI @dqmarkmagaoay , this library is no longer maintained, from my stressed test output, it can serve around 1.5K request/per hour. If it won’t fit your need, you need to use PDFSharp, or headless pdf, or a paid library.

@distantcam provided the solution. It works fine @Workaholic01. Those who may be checking this page in search of a solution, implement the solution by @distantcam. works for dotnet 6 … @distantcam Thanks for the solution

I’ve not written an Azure function but if you’re using wkhtmltopdf that implies you can at least load a DLL.

An exe might not work directly in an Azure function but what stops you from using more appropriate hosting to implement HTML to PDF as a web service and using it that way? Generation time is a few hundred ms for single page documents.

If that’s not good enough, Chromium is open source. Contribute a direct API and make yourself very popular.