Giter Site home page Giter Site logo

wkhtmltox's Introduction

WkHtmlToX

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

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);

Badges

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

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.

wkhtmltox's People

Contributors

adaskothebeast avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.