QuestPDF is an open-source, modern and battle-tested library that can help you with generating PDF documents by offering friendly, discoverable and predictable C# fluent API.
Перейти к файлу
Marcin Ziąbek 9c64a6bc8d
Merge pull request #128 from loxsmoke/main
Add nuget badges to readme.md file
2022-03-12 16:06:04 +01:00
.github Create codeql-analysis.yml 2022-01-21 03:11:22 +01:00
QuestPDF Fixed text rendering issues 2022-03-12 12:17:00 +01:00
QuestPDF.Examples Fixed text rendering issues 2022-03-12 12:17:00 +01:00
QuestPDF.ReportSample Renamed Decoration element 2022-01-16 17:08:57 +01:00
QuestPDF.UnitTests Decoration: improved implementation, fixed unit tests 2022-01-23 23:08:18 +01:00
.gitignore Library implementation 2021-01-16 01:31:39 +01:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2021-03-01 16:40:03 +01:00
LICENSE Initial commit 2021-01-04 23:47:18 +01:00
QuestPDF.sln Library implementation 2021-01-16 01:31:39 +01:00
SECURITY.md Update SECURITY.md 2021-07-18 18:10:55 +02:00
readme.md Update readme.md 2022-02-24 19:55:36 -08:00

readme.md

NuGet version NuGet

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);
        });
    }
}