Muonroi.ConvertHtmlToPdf 1.0.2

dotnet add package Muonroi.ConvertHtmlToPdf --version 1.0.2                
NuGet\Install-Package Muonroi.ConvertHtmlToPdf -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Muonroi.ConvertHtmlToPdf" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Muonroi.ConvertHtmlToPdf --version 1.0.2                
#r "nuget: Muonroi.ConvertHtmlToPdf, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Muonroi.ConvertHtmlToPdf as a Cake Addin
#addin nuget:?package=Muonroi.ConvertHtmlToPdf&version=1.0.2

// Install Muonroi.ConvertHtmlToPdf as a Cake Tool
#tool nuget:?package=Muonroi.ConvertHtmlToPdf&version=1.0.2                

DinkToPdf

.NET Core P/Invoke wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.

Install

Library can be installed through Nuget. Run command bellow from the package manager console:

PM> Install-Package DinkToPdf

Copy native library to root folder of your project. From there .NET Core loads native library when native method is called with P/Invoke. You can find latest version of native library here. Select appropriate library for your OS and platform (64 or 32 bit).

IMPORTANT

Library was NOT tested with IIS. Library was tested in console applications and with Kestrel web server both for Web Application and Web API .

Basic converter

Use this converter in single threaded applications.

Create converter:

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

Synchronized converter

Use this converter in multi threaded applications and web servers. Conversion tasks are saved to blocking collection and executed on a single thread.

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

Define document to convert

var doc = new HtmlToPdfDocument()
{
    GlobalSettings = {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Landscape,
        PaperSize = PaperKind.A4Plus,
    },
    Objects = {
        new ObjectSettings() {
            PagesCount = true,
            HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices  iaculis. Ut                               odio viverra, molestie lectus nec, venenatis turpis.",
            WebSettings = { DefaultEncoding = "utf-8" },
            HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
        }
    }
};

Convert

If Out property is empty string (defined in GlobalSettings) result is saved in byte array.

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

If Out property is defined in document then file is saved to disk:

var doc = new HtmlToPdfDocument()
{
    GlobalSettings = {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Margins = new MarginSettings() { Top = 10 },
        Out = @"C:\DinkToPdf\src\DinkToPdf.TestThreadSafe\test.pdf",
    },
    Objects = {
        new ObjectSettings()
        {
            Page = "http://google.com/",
        },
    }
};
converter.Convert(doc);

Dependency injection

Converter must be registered as singleton.

public void ConfigureServices(IServiceCollection services)
{
    // Add converter to DI
    services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 200 12/25/2024
1.0.1 160 12/25/2024
1.0.0 198 12/25/2024