|
|
@@ -1,222 +1,101 @@
|
|
|
-## QuestPDF Overview
|
|
|
+<a href="https://www.questpdf.com/" target="_blank">
|
|
|
+ <img src="https://github.com/QuestPDF/example-invoice/raw/main/images/logo.svg" width="300">
|
|
|
+</a>
|
|
|
|
|
|
-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.
|
|
|
+---
|
|
|
|
|
|
-## Features
|
|
|
+[](https://www.nuget.org/packages/QuestPDF/)
|
|
|
+[](https://github.com/QuestPDF/QuestPDF/stargazers)
|
|
|
+[](https://www.nuget.org/packages/QuestPDF/)
|
|
|
+[](https://www.nuget.org/packages/QuestPDF/)
|
|
|
+[](https://github.com/QuestPDF/QuestPDF/blob/main/LICENSE)
|
|
|
+[](https://github.com/sponsors/QuestPDF)
|
|
|
|
|
|
-**Rely on solid fundamentals** - This library is created specifically for designing and arranging document layouts, with full paging support. Alternative solutions, such as HTML-based converters, are not designed for this purpose and therefore are often unpredictable and do not produce desired results.
|
|
|
+QuestPDF is an open-source .NET library for PDF documents generation.
|
|
|
|
|
|
-**Work with organized self-explanatory code** - The entire process of implementing PDF document, takes place in your code. Free yourself from slow visual designers and strange technological limitations. Follow simple yet highly effective approaches to create maintainable, high-quality code.
|
|
|
+It offers a layouting engine designed with a 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 behavior 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.
|
|
|
|
|
|
-**Compose simple components into complex documents** - Do you remember the feeling when your code just works? When your ideas are becoming real without any effort? Working with simple, easy to understand, self-explanatory and highly composable layout elements is the key here!
|
|
|
+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.
|
|
|
|
|
|
-**Create and reuse components** - Feel no fear of complex documents! Create custom, reusable components and divide the document's layout into easy to maintain pieces. Inject data to customize content and use slots to enhance composability. Decide how complex approaches your solution needs and follow the best path.
|
|
|
+## Installation
|
|
|
|
|
|
-**Prototype with ease** - We understand that document generation is often tricky and require multiple iterations. The library offers additional prototyping tools such as random text generator or image placeholder element. By following best practices, you can develop a document without having data.
|
|
|
+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](https://www.nuget.org/packages/QuestPDF/).
|
|
|
|
|
|
-**Enjoy fast PDF generation** - QuestPDF is created upon SkiaSharp, a well-known graphical library, and converts your data into PDF documents. It offers a highly optimized layouting engine capable of generating over 1000 PDF files per minute per core. The entire process is thread-safe.
|
|
|
+```xml
|
|
|
+// Package Manager
|
|
|
+Install-Package QuestPDF
|
|
|
|
|
|
-## Learning resources
|
|
|
+// .NET CLI
|
|
|
+dotnet add package QuestPDF
|
|
|
|
|
|
-**[Release notes and roadmap](https://www.questpdf.com/documentation/releases.html)** - everything that is planned for future library iterations, description of new features and information about potential breaking changes.
|
|
|
-
|
|
|
-**[Getting started tutorial](https://www.questpdf.com/documentation/getting-started.html)** - a short and easy to follow tutorial showing how to design an invoice document under 200 lines of code.
|
|
|
-
|
|
|
-**[API Reference](https://www.questpdf.com/documentation/api-reference.html)** - a detailed description of behavior of all available components and how to use them with C# Fluent API.
|
|
|
+// Package reference in .csproj file
|
|
|
+<PackageReference Include="QuestPDF" Version="2022.3.0" />
|
|
|
+```
|
|
|
|
|
|
-**[Patterns and practices](https://www.questpdf.com/documentation/patterns-and-practices.html#document-metadata)** - everything that may help you designing great reports and reusable code that is easy to maintain.
|
|
|
+## Documentation
|
|
|
|
|
|
-## Example invoice
|
|
|
+[](https://www.questpdf.com/documentation/getting-started.html)
|
|
|
+A short and easy to follow tutorial showing how to design an invoice document under 200 lines of code.
|
|
|
|
|
|
-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](https://www.questpdf.com/documentation/getting-started.html).
|
|
|
+[](https://www.questpdf.com/documentation/api-reference.html)
|
|
|
+A detailed description of behavior of all available components and how to use them with C# Fluent API.
|
|
|
|
|
|
-
|
|
|
|
|
|
-Here you can find an example code showing how easy is to write and understand the fluent API.
|
|
|
+[](https://www.questpdf.com/documentation/patterns-and-practices.html#document-metadata)
|
|
|
+Everything that may help you designing great reports and create reusable code that is easy to maintain.
|
|
|
|
|
|
+## Simplicity is the key
|
|
|
|
|
|
-**General document structure** with header, content and footer:
|
|
|
+How easy it is to start and prototype with QuestPDF? Really easy thanks to its minimal API! Please analyse the code below:
|
|
|
|
|
|
```csharp
|
|
|
-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.
|
|
|
+using QuestPDF.Fluent;
|
|
|
+using QuestPDF.Helpers;
|
|
|
+using QuestPDF.Infrastructure;
|
|
|
|
|
|
-```csharp
|
|
|
-void ComposeHeader(IContainer container)
|
|
|
+// code in your main method
|
|
|
+Document.Create(container =>
|
|
|
{
|
|
|
- var titleTextStyle = TextStyle.Default.Size(20).SemiBold().Color(Colors.Blue.Medium);
|
|
|
-
|
|
|
- container.Row(row =>
|
|
|
+ container.Page(page =>
|
|
|
{
|
|
|
- {
|
|
|
- column.Item().Text($"Invoice #{Model.InvoiceNumber}", titleStyle);
|
|
|
-
|
|
|
- column.Item().Text(text =>
|
|
|
- {
|
|
|
- text.Span("Issue date: ", TextStyle.Default.SemiBold());
|
|
|
- text.Span($"{Model.IssueDate:d}");
|
|
|
- });
|
|
|
-
|
|
|
- column.Item().Text(text =>
|
|
|
- {
|
|
|
- text.Span("Due date: ", TextStyle.Default.SemiBold());
|
|
|
- text.Span($"{Model.DueDate:d}");
|
|
|
- });
|
|
|
- });
|
|
|
+ page.Size(PageSizes.A4);
|
|
|
+ page.Margin(2, Unit.Centimetre);
|
|
|
+ page.Background(Colors.White);
|
|
|
+ page.DefaultTextStyle(x => x.FontSize(20));
|
|
|
|
|
|
- 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.
|
|
|
-
|
|
|
-```csharp
|
|
|
-void ComposeContent(IContainer container)
|
|
|
-{
|
|
|
- container.PaddingVertical(40).column(column =>
|
|
|
- {
|
|
|
- column.Spacing(20);
|
|
|
+ page.Header()
|
|
|
+ .Text("Hello PDF!")
|
|
|
+ .SemiBold().FontSize(36).FontColor(Colors.Blue.Medium);
|
|
|
|
|
|
- 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:
|
|
|
-
|
|
|
-```csharp
|
|
|
-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);
|
|
|
- });
|
|
|
+ page.Content()
|
|
|
+ .PaddingVertical(1, Unit.Centimetre)
|
|
|
+ .Column(x =>
|
|
|
+ {
|
|
|
+ x.Spacing(20);
|
|
|
+
|
|
|
+ x.Item().Text(Placeholders.LoremIpsum());
|
|
|
+ x.Item().Image(Placeholders.Image(200, 100));
|
|
|
+ });
|
|
|
|
|
|
- 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)
|
|
|
+ page.Footer()
|
|
|
+ .AlignCenter()
|
|
|
+ .Text(x =>
|
|
|
{
|
|
|
- container.BorderBottom(1).BorderColor(Colors.Grey.Lighten2).PaddingVertical(5);
|
|
|
- }
|
|
|
- }
|
|
|
+ x.Span("Page ");
|
|
|
+ x.CurrentPageNumber();
|
|
|
+ });
|
|
|
});
|
|
|
-}
|
|
|
+})
|
|
|
+.GeneratePdf("hello.pdf");
|
|
|
```
|
|
|
|
|
|
-```csharp
|
|
|
-void ComposeComments(IContainer container)
|
|
|
-{
|
|
|
- container.ShowEntire().Background(Colors.Grey.Lighten3).Padding(10).column(message =>
|
|
|
- {
|
|
|
- message.Spacing(5);
|
|
|
- message.Item().Text("Comments", TextStyle.Default.Size(14).SemiBold());
|
|
|
- message.Item().Text(Model.Comments);
|
|
|
- });
|
|
|
-}
|
|
|
-```
|
|
|
+And compare it to the produced PDF file:
|
|
|
|
|
|
+<img src="https://github.com/QuestPDF/example-invoice/raw/main/images/minimal-api.png" width="300px">
|
|
|
|
|
|
-**The address details section** is implemented using components. This way the code can be easily reused for both seller and customer:
|
|
|
+## Are you ready for more?
|
|
|
|
|
|
-```csharp
|
|
|
-public class AddressComponent : IComponent
|
|
|
-{
|
|
|
- private string Title { get; }
|
|
|
- private Address Address { get; }
|
|
|
+The Fluent API of QuestPDF scales really well. It is easy to create and maintain even most complex documents. Read [the Getting started tutorial](https://www.questpdf.com/documentation/getting-started.html) to learn QuestPDF basics and implement an invoice under 200 lines of code. You can also investigate and play with the code from [the example repository](https://github.com/QuestPDF/example-invoice).
|
|
|
|
|
|
- public AddressComponent(string title, Address address)
|
|
|
- {
|
|
|
- Title = title;
|
|
|
- Address = address;
|
|
|
- }
|
|
|
-
|
|
|
- public void Compose(IContainer container)
|
|
|
- {
|
|
|
- container.ShowEntire().column(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);
|
|
|
- });
|
|
|
- }
|
|
|
-}
|
|
|
-```
|
|
|
+<img src="https://github.com/QuestPDF/example-invoice/raw/main/images/invoice.png" width="400px">
|