AdaskoTheBeAsT.WkHtmlToX

AdaskoTheBeAsT.WkHtmlToX c# wrapper


Keywords
License
MIT
Install
Install-Package AdaskoTheBeAsT.WkHtmlToX -Version 7.1.0

Documentation

WkHtmlToX

C# wrapper for wkhtmltopdf.org Html to Pdf and Image library.

Badges

CodeFactor Total alerts Build Status Azure DevOps tests Azure DevOps coverage Quality Gate Status Sonar Tests Sonar Test Count Sonar Test Execution Time Sonar Coverage Nuget

Usage

In web api (in combination with SimpleInjector) registration should be as follows:

   // for simplicity only version for win here
    var configuration = new WkHtmlToXConfiguration((int)Environment.OSVersion.Platform, null);
    _container.RegisterInstance(configuration);
    _container.RegisterSingleton<IWkHtmlToXEngine, WkHtmlToXEngine>();
    _container.RegisterSingleton<IPdfConverter, PdfConverter>();
    _container.RegisterInitializer<IWkHtmlToXEngine>(e => e.Initialize());

In command line application:

    // for simplicity only version for win here
    var configuration = new WkHtmlToXConfiguration((int)Environment.OSVersion.Platform, null);
    using (var engine = new WkHtmlToXEngine(configuration))
    {
        engine.Initialize();

        var converter = new PdfConverter(engine);
    }

Method for conversion to pdf takes 3 parameters. First is settings object in which there is possibility to pass html content to be converted. In second parameter you need to pass func which based on length will create stream. In third you need to pass CancellationToken.

Second parameter is tricky but such construction allows to use for example Microsoft.IO.RecyclableMemoryStream to reuse block of memories. Then whole conversion can look like this

    // doc settings object created earlier
    Stream? stream = null;
    var converted = await _pdfConverter.ConvertAsync(
        doc,
        length =>
        {
            stream = _recyclableMemoryStreamManager.GetStream(
                Guid.NewGuid(),
                "wkhtmltox",
                length);
            return stream;
        },
        _httpContextAccessor.HttpContext?.RequestAborted ?? CancellationToken.None);
    stream!.Position = 0;
    if (converted)
    {
        var result = new FileStreamResult(stream, "application/pdf")
        {
            FileDownloadName = "sample.pdf",
        };

        return result;
    }

WkHtmlToX native lib comes with support for following operation system flavours:

  • WinX64,
  • WinX86,
  • OsxX64,
  • AmazonLinux2,
  • Centos6,
  • Centos7,
  • Centos8,
  • Debian9X64,
  • Debian9X86,
  • Debian10X64,
  • Debian10X86,
  • OpenSuseLeap15,
  • Ubuntu1404X64,
  • Ubuntu1404X86,
  • Ubuntu1604X64,
  • Ubuntu1604X86,
  • Ubuntu1804X64,
  • Ubuntu1804X86,
  • Ubuntu2004X64,

For linux you can set it using second parameter in config.

    var configuration = new WkHtmlToXConfiguration((int)PlatformID.Unix, WkHtmlToXRuntimeIdentifier.Ubuntu2004X64);

Influencers

Library is based on wrapper (DinkToPdf)[https://github.com/rdvojmoc/DinkToPdf]. Interoperability was totally reworked and now it is under tests to see if leaking memory can be avoided.