Theoistic.RazorPDF 1.3.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Theoistic.RazorPDF --version 1.3.3                
NuGet\Install-Package Theoistic.RazorPDF -Version 1.3.3                
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.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Theoistic.RazorPDF --version 1.3.3                
#r "nuget: Theoistic.RazorPDF, 1.3.3"                
#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.
// Install Theoistic.RazorPDF as a Cake Addin
#addin nuget:?package=Theoistic.RazorPDF&version=1.3.3

// Install Theoistic.RazorPDF as a Cake Tool
#tool nuget:?package=Theoistic.RazorPDF&version=1.3.3                

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. 
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 523 11/7/2023
1.3.3 273 11/7/2023
1.3.2 248 11/7/2023
1.3.1 261 11/7/2023
1.3.0 409 10/20/2023
1.2.0 316 10/19/2023
1.1.0 393 10/18/2023
1.0.0 379 10/17/2023