Skip to content

ldsenow/PdfTemplateEngine

Repository files navigation

PdfTemplateEngine

A C# PDF generator using Razor and Playwright. The idea is to use the compiled html from Razor Component and send it to Playwright for PDF generation.

Warning

Nuget packages aren't available at this stage. It is in early stage of development. However, welcome to raise issues or suggestions.

Configuration

builder.Services.AddPdfTemplateEngine(config =>
{
    config.UseRazorRenderer();
    config.UsePlaywrightGenerator(options =>
    {
        options.MinInstances = 1;
        options.MaxInstances = 5;
        options.IdleTimeoutMinutes = 30;
    });
});

Usage (ASP.NET Core Minimal API)

Generate PDF
app.MapGet("/render-pdf", async (IPdfGenerator pdfGenerator) =>
{
    var pdfBytes = await pdfGenerator.Generate<SampleComponent, SampleComponentModel>(new SampleComponentModel
    {
        Text = "Hello, world!"
    });

    return Results.File(
        fileDownloadName: $"output_{DateTime.Now.Ticks}.pdf",
        fileContents: pdfBytes,
        contentType: "application/pdf");
})
.WithOpenApi();
Generate HTML
app.MapGet("/render-html", async (
    [AsParameters] SampleComponentModel model, 
    [FromServices] IPdfRenderer renderer) =>
{
    var html = await renderer.Render<SampleComponent, SampleComponentModel>(model);

    return Results.Content(html, contentType: "text/html");
})
.WithOpenApi();

Note

SampleComponent is a Razor component and SampleComponentModel is a POCO class as the model

Tip

The model can be passed an API endpoint parameter and validated etc..

Important

Razor Component's features won't be fully supported. However, using as a static html templating engine, it is good enough.

About

A C# PDF generator using Razor and Playwright

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published