HakanL / WkHtmlToPdf-DotNet

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
GNU Lesser General Public License v3.0
367 stars 66 forks source link

[Help] error 80010106 - Worker Service #42

Closed sajermann closed 3 years ago

sajermann commented 3 years ago

Hi, I am having trouble with implantation with Work Service

My Program.cs

  public class Program
  {
    [STAThread]
    public static void Main(string[] args)
    {
      CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
              services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
              services.AddHostedService<Worker>();
            });
  }

My Worker.cs

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {

      var converter = new SynchronizedConverter(new PdfTools());
      var doc = new HtmlToPdfDocument()
      {
        GlobalSettings = {
          ColorMode = ColorMode.Color,
          Orientation = Orientation.Portrait,
          PaperSize = PaperKind.A4,
          Margins = new MarginSettings() { Top = 10 },
          Out = $"‪C:\\Users\\b.sajermann.da.silva\\Desktop\\test.pdf",
          },
        Objects = {
          new ObjectSettings()
          {
              HtmlContent = @"<b>Lorem</b><div style='display:none'>bruno</div> ",

          },
        }
      };

      converter.Convert(doc);
      Console.WriteLine("Hello World!");
      Console.ReadKey();
    }

Error in Console:

Qt: Could not initialize OLE (error 80010106)
QPainter::begin(): Returned false

I've already tested it with and without [STAThread]

Thanks!

HakanL commented 3 years ago

It seems that you're instantiating the Converter two times, try to remove one of them and also make sure your ExecuteAsync is actually called.

sajermann commented 3 years ago

I change:

My Worker.cs

public class Worker : BackgroundService
  {
    private readonly IConverter _converter;

    public Worker(IConverter converter)
    {
      _converter = converter;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
      var doc = new HtmlToPdfDocument()
      {
        GlobalSettings = {
          ColorMode = ColorMode.Color,
          Orientation = Orientation.Portrait,
          PaperSize = PaperKind.A4,
          Margins = new MarginSettings() { Top = 10 },
          Out = $"‪C:\\Users\\b.sajermann.da.silva\\Desktop\\test.pdf",
          },
        Objects = {
          new ObjectSettings()
          {
              HtmlContent = @"<b>Lorem</b><div style='display:none'>bruno</div> ",
          },
        }
      };

      _converter.Convert(doc);
      Console.WriteLine("Hello World!");
      Console.ReadKey();
    }
  }

And the same error occurs...

HakanL commented 3 years ago

Are you sure those just aren't warnings? Do you get an exception? Try to compare with the samples.

sajermann commented 3 years ago

Yes, don't have exception, only print this errors in console and don't generate pdf...

But thanks for your attention, I try other solutions;

Thank's very much!

hidegh commented 2 months ago

Since the message results in no PDF being generated, the ticket should not have been closed!

HakanL commented 2 months ago

Since the message results in no PDF being generated, the ticket should not have been closed!

It was most likely because the OP created a new instance of the SynchronizedConverter each time, or a missing external dependency. The samples in this project show how it's supposed to be used.