PsdUtilities.RazorPdf 1.0.0

dotnet add package PsdUtilities.RazorPdf --version 1.0.0
                    
NuGet\Install-Package PsdUtilities.RazorPdf -Version 1.0.0
                    
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="PsdUtilities.RazorPdf" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PsdUtilities.RazorPdf" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PsdUtilities.RazorPdf" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PsdUtilities.RazorPdf --version 1.0.0
                    
#r "nuget: PsdUtilities.RazorPdf, 1.0.0"
                    
#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.
#:package PsdUtilities.RazorPdf@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PsdUtilities.RazorPdf&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PsdUtilities.RazorPdf&version=1.0.0
                    
Install as a Cake Tool

PsdUtilities.RazorPdf

PsdUtilities.RazorPdf - a simple wrapper around Razor HtmlRenderer and Microsoft.Playwright library. Its designed to be used in a hosted app, due to the fact that it uses a hosted service to initialize the required components.

Setup

Prematurely, you will have to install playwright dependencies (browser binaries), which conclude to the size of ~350MB in total. You can use the library's utility for that:

// not a part of the application itself, just a pre-requirement. Run seperately from the app.
// .NET version matters here!
RazorPdfUtility.InstallRequiredDependencies();

You will see the installation progress in the console.

How to use?

You will need to have some kind of hosted app, in my example i will use a regular HostApplicationBuilder, in a case of a WebAPI, use the appropriate builder.

var builder = Host.CreateApplicationBuilder();

builder.Services.AddRazorPdfConverter(); // important !

var app = builder.Build();

await app.RunAsync();

You should prefer app.RunAsync() over the typical app.Run(), because as mentioned before, the library uses a hosted service which asynchronously initializes the underlying frameworks.<br><br>

After that, you can inject the IRazorPdfConverter into any of your services, in my example its going to be a hosted service.

class MyHostedService : IHostedService
{
    private readonly IRazorPdfConverter _razorPdfConverter;

    public MyHostedService(IRazorPdfConverter razorPdfConverter)
    {
        _razorPdfConverter = razorPdfConverter;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

After that, you will need to actually register your service in the DI.

builder.Services.AddHostedService<MyHostedService>();

<br><br> Now, we can actually utilize the converter. We will need a razor component in order to render the html. In my case, i will use an imaginary invoice service for generating invoice models

class MyHostedService : IHostedService
{
    private readonly IRazorPdfConverter _razorPdfConverter;
    private readonly IInvoiceService _invoiceService;

    public MyHostedService(IRazorPdfConverter razorPdfConverter, IInvoiceService invoiceService)
    {
        _razorPdfConverter = razorPdfConverter;
        _invoiceService = invoiceService;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        var invoice = _invoiceService.GenerateInvoice();
        var parameters = new Dictionary<string, object?>()
        {
            { "Invoice", invoice }
        };

        var bytes = await _razorPdfConverter.GeneratePdfAsync<InvoiceView>(parameters);
        await File.WriteAllBytesAsync("output.pdf", bytes, cancellationToken);
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

You can provide PdfOptions and a set of parameters for your razor component in the GeneratePdfAsync method.

Example of a Razor component

Here is an example of a very simple razor component, which in my case is an invoice visualizer.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8"/>
    <title>Invoice @Invoice.Number</title>
</head>

<body>
	<p>
		Invoice #: @Invoice.Number
		<br />
		Created: @Invoice.IssueDate.ToString("MMMM dd, yyyy")
		<br />
		Due: @Invoice.DueDate.ToString("MMMM dd, yyyy")
	</p>
</body>

</html>

@code {
    [Parameter] public Invoice Invoice { get; set; }
}
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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0 214 8/21/2025