Generate and edit PDF documents in your .NET applications using the open-source QuestPDF library and its C# Fluent API. Build invoices, reports and data exports with ease.

www.questpdf.com

Topics
#html #csharp #export #pdf #library #nuget #report #dotnetcore #dotnet #generation #invoice #document #form #generation

MarcinZiabek 662a55e21c Implemented text shaping prototype 3 years ago
.github ba409a8712 Create codeql-analysis.yml 3 years ago
QuestPDF 662a55e21c Implemented text shaping prototype 3 years ago
QuestPDF.Examples 662a55e21c Implemented text shaping prototype 3 years ago
QuestPDF.ReportSample 68df390d0e Element renaming + custom text format 3 years ago
QuestPDF.UnitTests 662a55e21c Implemented text shaping prototype 3 years ago
.gitignore 8326446446 Library implementation 4 years ago
CODE_OF_CONDUCT.md 6b6d2d7fa3 Create CODE_OF_CONDUCT.md 4 years ago
LICENSE 3e3c464974 Initial commit 5 years ago
QuestPDF.sln 8326446446 Library implementation 4 years ago
SECURITY.md 20fe49dd01 Update SECURITY.md 4 years ago
readme.md 63288dfb60 Updated examples 3 years ago

readme.md

QuestPDF presents a new approach to PDF document generation. Unlike other libraries, it does not rely on the HTML-to-PDF conversion which in many cases is not reliable. Instead, it implements its own layouting engine that is optimized to cover all paging-related requirements. Then, everything is rendered using the SkiaSharp library (a Skia port for .NET, used in Chrome, Android, MAUI, etc.).

I have designed this layouting engine with full paging support in mind. The document consists of many simple elements (e.g. border, background, image, text, padding, table, grid etc.) that are composed together to create more complex structures. This way, as a developer, you can understand the behaviour of every element and use them with full confidence. Additionally, the document and all its elements support paging functionality. For example, an element can be moved to the next page (if there is not enough space) or even be split between pages like table's rows.

Support QuestPDF

All great frameworks and libraries started from zero. Please help me make QuestPDF a commonly known library and an obvious choice in case of generating PDF documents. Please give it a start ⭐ and share with your colleagues 💬👨‍💻.

Installation

The library is available as a nuget package. You can install it as any other nuget package from your IDE, try to search by QuestPDF. You can find package details on this webpage.

Install-Package QuestPDF

Documentation

Release notes and roadmap - everything that is planned for future library iterations, description of new features and information about potential breaking changes.

Getting started tutorial - a short and easy to follow tutorial showing how to design an invoice document under 200 lines of code.

API Reference - a detailed description of behavior of all available components and how to use them with C# Fluent API.

Patterns and practices - everything that may help you designing great reports and reusable code that is easy to maintain.

Example invoice

Do you believe that creating a complete invoice document can take less than 200 lines of code? We have prepared for you a step-by-step instruction that shows every detail of this implementation and describes the best patterns and practices.

For tutorial, documentation and API reference, please visit the QuestPDF documentation.

Here you can find an example code showing how easy is to write and understand the fluent API.

General document structure with header, content and footer:

public void Compose(IDocumentContainer container)
{
    container
        .Page(page =>
        {
            page.Margin(50);
            
            page.Header().Element(ComposeHeader);
            page.Content().Element(ComposeContent);
            
            page.Footer().AlignCenter().Text(x =>
            {
                x.CurrentPageNumber();
                x.Span(" / ");
                x.TotalPages();
            });
        });
}

The header area consists of basic invoice information along with a logo placeholder.

void ComposeHeader(IContainer container)
{
    var titleTextStyle = TextStyle.Default.Size(20).SemiBold().Color(Colors.Blue.Medium);
    
    container.Row(row =>
    {
        row.RelativeColumn().Stack(stack =>
        {
            stack.Item().Text($"Invoice #{Model.InvoiceNumber}", titleStyle);

            stack.Item().Text(text =>
            {
                text.Span("Issue date: ", TextStyle.Default.SemiBold());
                text.Span($"{Model.IssueDate:d}");
            });

            stack.Item().Text(text =>
            {
                text.Span("Due date: ", TextStyle.Default.SemiBold());
                text.Span($"{Model.DueDate:d}");
            });
        });
        
        row.ConstantColumn(100).Height(50).Placeholder();
    });
}

Implementation of the content area that contains seller and customer details, then listing of all bought products, then a comments section.

void ComposeContent(IContainer container)
{
    container.PaddingVertical(40).Stack(column => 
    {
        column.Spacing(20);
        
        column.Item().Row(row =>
        {
            row.RelativeColumn().Component(new AddressComponent("From", Model.SellerAddress));
            row.ConstantColumn(50);
            row.RelativeColumn().Component(new AddressComponent("For", Model.CustomerAddress));
        });

        column.Item().Element(ComposeTable);

        var totalPrice = Model.Items.Sum(x => x.Price * x.Quantity);
        
        column
            .Item()
            .PaddingRight(5)
            .AlignRight()
            .Text($"Grand total: {totalPrice}$", TextStyle.Default.SemiBold());

        if (!string.IsNullOrWhiteSpace(Model.Comments))
            column.Item().PaddingTop(25).Element(ComposeComments);
    });
}

The table and comments codes are extracted into separate methods to increase clarity:

void ComposeTable(IContainer container)
{
    var headerStyle = TextStyle.Default.SemiBold();
    
    container.Table(table =>
    {
        table.ColumnsDefinition(columns =>
        {
            columns.ConstantColumn(25);
            columns.RelativeColumn(3);
            columns.RelativeColumn();
            columns.RelativeColumn();
            columns.RelativeColumn();
        });
        
        table.Header(header =>
        {
            header.Cell().Text("#", headerStyle);
            header.Cell().Text("Product", headerStyle);
            header.Cell().AlignRight().Text("Unit price", headerStyle);
            header.Cell().AlignRight().Text("Quantity", headerStyle);
            header.Cell().AlignRight().Text("Total", headerStyle);
            
            header.Cell().ColumnSpan(5)
                  .PaddingVertical(5).BorderBottom(1).BorderColor(Colors.Black);
        });
        
        foreach (var item in Model.Items)
        {
            table.Cell().Element(CellStyle).Text(Model.Items.IndexOf(item) + 1);
            table.Cell().Element(CellStyle).Text(item.Name);
            table.Cell().Element(CellStyle).AlignRight().Text($"{item.Price}$");
            table.Cell().Element(CellStyle).AlignRight().Text(item.Quantity);
            table.Cell().Element(CellStyle).AlignRight().Text($"{item.Price * item.Quantity}$");
            
            static IContainer CellStyle(IContainer container)
            {
                container.BorderBottom(1).BorderColor(Colors.Grey.Lighten2).PaddingVertical(5);
            }
        }
    });
}
void ComposeComments(IContainer container)
{
    container.ShowEntire().Background(Colors.Grey.Lighten3).Padding(10).Stack(message => 
    {
        message.Spacing(5);
        message.Item().Text("Comments", TextStyle.Default.Size(14).SemiBold());
        message.Item().Text(Model.Comments);
    });
}

The address details section is implemented using components. This way the code can be easily reused for both seller and customer:

public class AddressComponent : IComponent
{
    private string Title { get; }
    private Address Address { get; }

    public AddressComponent(string title, Address address)
    {
        Title = title;
        Address = address;
    }
    
    public void Compose(IContainer container)
    {
        container.ShowEntire().Stack(column =>
        {
            column.Spacing(5);

            column
                .Item()
                .BorderBottom(1)
                .PaddingBottom(5)
                .Text(Title, TextStyle.Default.SemiBold());
            
            column.Item().Text(Address.CompanyName);
            column.Item().Text(Address.Street);
            column.Item().Text($"{Address.City}, {Address.State}");
            column.Item().Text(Address.Email);
            column.Item().Text(Address.Phone);
        });
    }
}