Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/ WKPDFGen Public archive

C# wrapper for wkhtmltopdf library for cross-platform generating PDF files from HTML

License

Notifications You must be signed in to change notification settings

BigDaddy1337/WKPDFGen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WKPDFGen

Nuget (with prereleases)

C# wrapper for wkhtmltopdf library for cross-platform generating PDF files from HTML

Usage

Download package for your OS architecture from https://wkhtmltopdf.org/downloads.html

Extract files and find OS specific library file and get path of it.

Alternatively you can check examples folder with already extracted libraries for different OS's.

// Register wrapper with path to native library file for your OS

public void ConfigureServices(IServiceCollection services)
{
    services.AddWkPdfGenerator(options =>
    {
        var nativeLibsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "0.12.6");
        
        options.LinuxLibPath = Path.Combine(nativeLibsPath, "libwkhtmltox.so.0.12.6");
        options.WindowsLibPath = Path.Combine(nativeLibsPath, "libwkhtmltox.dll");
        options.OsxLibPath = Path.Combine(nativeLibsPath, "libwkhtmltox.0.12.6.dylib");
    });
}
// Create configuration once before using converter

private readonly WKPDFGenConfiguration pdfConfig = new(
    new PdfSettings
    {
        DocumentTitle = "Example PDF",
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Copies = 1,
        PagesCount = true,
        LoadSettings = new LoadSettings
        {
            BlockLocalFileAccess = false
        },
        HeaderSettings = new HeaderSettings
        {
            FontSize = 14,
            Line = false
        },
        FooterSettings = new FooterSettings
        {
            FontSize = 14,
            Line = false,
            Spacing = 2.0
        }
    }
);

// Inject converter form DI

public ExampleClass(IWkHtmlToPdfConverter pdfConverter)
{
    this.pdfConverter = pdfConverter;
}

// Use the most convenient API 

private async Task CreatePdf(string html, CancellationToken stoppingToken)
{
    var fileStream = await pdfConverter.CreateAsync(html, pdfConfig, stoppingToken);
            
    await using var writeFileStream = File.OpenWrite("example.pdf");
            
    await fileStream.CopyToAsync(writeFileStream, stoppingToken);
}

private async Task CreatePdfOnDisk(string html, CancellationToken stoppingToken)
{
    const string path = "example-on-disk.pdf";
    
    await pdfConverter.CreateOnDiskAsync(html, path, pdfConfig, stoppingToken);
}

Check examples folder for more details.

About

C# wrapper for wkhtmltopdf library for cross-platform generating PDF files from HTML

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published