Theoistic.RazorPDF 1.3.4

dotnet add package Theoistic.RazorPDF --version 1.3.4
                    
NuGet\Install-Package Theoistic.RazorPDF -Version 1.3.4
                    
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="Theoistic.RazorPDF" Version="1.3.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Theoistic.RazorPDF" Version="1.3.4" />
                    
Directory.Packages.props
<PackageReference Include="Theoistic.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 Theoistic.RazorPDF --version 1.3.4
                    
#r "nuget: Theoistic.RazorPDF, 1.3.4"
                    
#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 Theoistic.RazorPDF@1.3.4
                    
#: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=Theoistic.RazorPDF&version=1.3.4
                    
Install as a Cake Addin
#tool nuget:?package=Theoistic.RazorPDF&version=1.3.4
                    
Install as a Cake Tool

RazorPDF

RazorPDF is a powerful library for .NET that provides capabilities to build PDF documents using Razor views. With RazorPDF, you can create complex PDF documents, render HTML content, and apply styles, all with the familiar Razor syntax and .NET environment. It's perfect for generating invoices, reports, forms, and more!

Features

  • Generate PDFs using familiar Razor syntax and views.
  • Inject CSS for styling your PDFs.
  • Comprehensive PDF settings like compression, size, orientation, and more.
  • Asynchronous methods for building PDFs.

Installation

You can add RazorPDF to your project via the NuGet package manager. Use the following command in your Package Manager Console:

Install-Package Theoistic.RazorPDF

Configuration

To use RazorPDF in your project, you need to configure the services and application builder typically in your Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPDF();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRazorPDF();
}

Quick Start

Here's a quick demonstration of how you can generate a PDF from a Razor view.

First, create your Razor view (e.g., PDFViews/NicePDF.cshtml). This view will contain the HTML structure and content that will be rendered in your PDF.

@model NicePDFModel

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>My Nice PDF</h1>
    <table>
        @foreach (var item in Model.Items)
        {
            <tr>
                <td>@item.Name</td>
                <td>@item.Value</td>
            </tr>
        }
    </table>
</body>
</html>

In your controller or service, use PDFBuilder to generate the PDF:

public async Task CreatePDF()
{
    var pdf = await new PDFBuilder()
        .Settings(x =>
        {
            x.UseCompression = true;
        })
        .InjectCSS("wwwroot/PDFStyle.css")
        .RazorView("PDFViews/NicePDF", new NicePDFModel { 
            Items = new List<NicePDFModel.Item> {
                new NicePDFModel.Item { Name = "Something", Value = "10.42" },
                new NicePDFModel.Item { Name = "Something else", Value = "50.42" },
                new NicePDFModel.Item { Name = "Something more", Value = "21.42" },
            }
        })
        .BuildAsync();

    System.IO.File.WriteAllBytes("test.pdf", pdf);
}

This will create a PDF document from your Razor view with the data you provided and save it as test.pdf.

Style

If you need to have a specific style, I would recommend using a CSS file and injecting it using the InjectCSS method. the InjectCSS method takes a string as a parameter, this string is the (relative) path to the CSS file you want to inject. since we cannot use relative paths, its converted to absolute path and the link stylesheet is injected right above the closing head tag. Once the CSS has been injected with a full absolute path, it can reference relative images and fonts.

Contributing

Contributions to the RazorPDF library are welcome! If you're interested in improving RazorPDF

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.3.4 740 11/7/2023
1.3.3 317 11/7/2023
1.3.2 317 11/7/2023
1.3.1 330 11/7/2023
1.3.0 456 10/20/2023
1.2.0 362 10/19/2023
1.1.0 442 10/18/2023
1.0.0 428 10/17/2023