Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 2.33 KB

README.md

File metadata and controls

75 lines (54 loc) · 2.33 KB

Introduction

WeasyPrint Wrapper for .Net on Windows to generate pdf from html. It uses WeasyPrint to generate pdf from html without any extra installation and setup on Windows.

Gtb.WeasyPrint simplifies the using of WeasyPrint on Windows, it is a minor change from Balbarak.WeasyPrint It was forked to use in a web project which is executed under an app pool with no user profile, so I don't want to save files to appsettings.

Getting started

Installation

As nuget package, the tb version is not on nuget.org, so first add a local repository and copy the .nupkg to it.

PM> Install-Package Gtb.WeasyPrint

It is also possible to install as dll reference, or even as a project reference.

Usage

From html text

using Gtb.WeasyPrint
using System.IO;

using (WeasyPrintClient client = new WeasyPrintClient(@"E:\path\to\weasy"))
{
    var html = "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>";

    var binaryPdf = await client.GeneratePdfAsync(html);

    File.WriteAllBytes("result.pdf",binaryPdf);
}

From html file

using (WeasyPrintClient client = new WeasyPrintClient(@"E:\path\to\weasy"))
{
    var input = @"path\to\input.html";
    var output = @"path\to\output.pdf";

    await client.GeneratePdfAsync(input,output);
}

Watch output and errors

using (WeasyPrintClient client = new WeasyPrintClient(@"E:\path\to\weasy"))
{
    var input = @"path\to\input.html";
    var output = @"path\to\output.pdf";

    client.OnDataError += OnDataError;
    client.OnDataOutput += OnDataOutput;

    await client.GeneratePdfAsync(input,output);
}

private void OnDataOutput(OutputEventArgs e)
{
    Console.WriteLine(args.Data);
}

private void OnDataError(OutputEventArgs e)
{
    Console.WriteLine(e.Data);
}

Third Parties