PsdUtilities.RazorPdf
1.0.0
dotnet add package PsdUtilities.RazorPdf --version 1.0.0
NuGet\Install-Package PsdUtilities.RazorPdf -Version 1.0.0
<PackageReference Include="PsdUtilities.RazorPdf" Version="1.0.0" />
<PackageVersion Include="PsdUtilities.RazorPdf" Version="1.0.0" />
<PackageReference Include="PsdUtilities.RazorPdf" />
paket add PsdUtilities.RazorPdf --version 1.0.0
#r "nuget: PsdUtilities.RazorPdf, 1.0.0"
#:package PsdUtilities.RazorPdf@1.0.0
#addin nuget:?package=PsdUtilities.RazorPdf&version=1.0.0
#tool nuget:?package=PsdUtilities.RazorPdf&version=1.0.0
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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.19)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.8)
- Microsoft.Extensions.Logging (>= 9.0.8)
- Microsoft.Playwright (>= 1.54.0)
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 |