DinkToPdf Documentation

repository·master·Indexed 22 days ago

https://github.com/rdvojmoc/dinktopdf

A .NET Core P/Invoke wrapper for the wkhtmltopdf library used to convert HTML strings or URLs into PDF documents via the Webkit engine. It provides BasicConverter for single-threaded applications and SynchronizedConverter for multi-threaded environments, requiring native wkhtmltopdf libraries to be present in the project root.

Tokens
1.2K
Snippets
5
Records
6
Agent score
29%

What's inside DinkToPdf

  1. Choose between BasicConverter and SynchronizedConverter

    master

    Depending on your application's threading model, choose one of the following converters:

    BasicConverter

    Use this in single-threaded applications.

    var converter = new BasicConverter(new PdfTools());

    SynchronizedConverter

    Use this in multi-threaded applications and web servers. It uses a blocking collection to queue conversion tasks and executes them on a single thread to ensure thread safety.

    var converter = new SynchronizedConverter(new PdfTools());
    // For single-threaded apps
    var converter = new BasicConverter(new PdfTools());
    
    // For multi-threaded/web apps
    var converter = new SynchronizedConverter(new PdfTools());
  2. Register DinkToPdf with Dependency Injection

    master

    When using Dependency Injection (e.g., in ASP.NET Core), the converter must be registered as a Singleton.

    public void ConfigureServices(IServiceCollection services)
    {
        // Register the converter as a singleton
        services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
    }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
    }
  3. Configure native libraries for DinkToPdf

    master

    DinkToPdf is a P/Invoke wrapper, so you must manually copy the native wkhtmltopdf libraries to the root folder of your project. .NET Core will load these libraries when native methods are called.

    1. Download the appropriate native library for your OS and platform (64-bit or 32-bit) from the official repository.
    2. Place the files in your project's root folder.

    Note: The library has been tested with Kestrel (Web Application and Web API) and console applications, but has NOT been tested with IIS.

  4. Define an HtmlToPdfDocument for conversion

    master

    To convert content, you must define an HtmlToPdfDocument object. This object contains GlobalSettings (like paper size and orientation) and a collection of Objects (the actual HTML content or URLs to render).

    Key settings include:

    • GlobalSettings.ColorMode: Set to ColorMode.Color or ColorMode.GrayScale.
    • GlobalSettings.Orientation: e.g., Orientation.Landscape or Orientation.Portrait.
    • GlobalSettings.PaperSize: e.g., PaperKind.A4.
    • Objects.HtmlContent: The raw HTML string to convert.
    • Objects.Page: A URL to convert.
    • Objects.WebSettings.DefaultEncoding: e.g., "utf-8".
    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings = {
            ColorMode = ColorMode.Color,
            Orientation = Orientation.Landscape,
            PaperSize = PaperKind.A4Plus,
        },
        Objects = {
            new ObjectSettings() {
                PagesCount = true,
                HtmlContent = @"<h1>Hello World</h1>",
                WebSettings = { DefaultEncoding = "utf-8" },
                HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
            }
        }
    };
  5. Convert HTML to PDF byte array or file

    master

    Use the Convert method on your converter instance. The output destination depends on the Out property in GlobalSettings:

    Convert to Byte Array

    If the Out property is an empty string (or not defined), the method returns a byte[] containing the PDF data.

    byte[] pdf = converter.Convert(doc);

    Convert to File

    If the Out property is defined with a file path, the PDF will be saved directly to that location on disk.

    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings = {
            Out = @"C:\path\to\output.pdf",
            // ... other settings
        },
        Objects = { new ObjectSettings() { Page = "http://google.com/" } }
    };
    converter.Convert(doc);
    // To byte array
    byte[] pdf = converter.Convert(doc);
    
    // To file (if doc.GlobalSettings.Out is set)
    converter.Convert(doc);