Location>code7788 >text

NET tool library to efficiently generate PDF documents

Popularity:782 ℃/2024-09-26 11:19:42

preamble

QuestPDF is an open source .NET library for generating PDF documents . Using the C# Fluent API approach can simplify development , reduce errors and improve efficiency . Use it can easily generate PDF reports , invoices , export files and so on.

Projects

QuestPDF is a revolutionary open source .NET library that revolutionizes the way we generate PDF documents.

The core idea of QuestPDF is to use declarative C# code to describe the structure and style of PDF documents.

This approach is very similar to the way we build web pages using HTML and CSS, and we were able to get started quickly and easily create complex PDF documents.

 

Project Characteristics

  • Simple and Powerful API: QuestPDF's API is designed to be intuitive and easy to use, with little to no documentation required to get started. Through a series of smooth method chains, complex document structures can be described in a small amount of code.
  • High Performance: QuestPDF excels in performance and is able to generate large documents quickly. Even complex reports containing thousands of pages are handled with ease, thanks to its efficient internal algorithms and optimized architecture.
  • Cross-platform support: QuestPDF works flawlessly with applications running on Windows, macOS, or Linux. The cross-platform nature makes it especially useful for building cloud-native applications.
  • Zero Dependencies: Unlike other PDF libraries, QuestPDF does not require any external dependencies. There is no need to install additional fonts or use third-party libraries, which greatly simplifies the deployment and maintenance process.
  • Rich Element Support: QuestPDF supports a wide range of document elements, including text, images, tables and barcodes. It even supports complex charting so that documents are more expressive.
  • Dynamic Content and Intelligent Pagination: QuestPDF can handle dynamically generated content and solve pagination problems automatically. No more need to worry about content overflow or improper paging.
  • Powerful debugging tools: QuestPDF provides powerful debugging tools to quickly locate and solve problems. You can also visualize the document structure and easily find layout errors.

QuestPDF simplifies the PDF generation process, providing a better development experience and higher productivity than traditional PDF generation libraries.

Project use

Installing the QuestPDF Nuget Package

Search for the QuestPDF package to install, as shown below

Project Examples

Create a WebAPI project to use QuestPDF to quickly generate PDF files.

1, need to add configuration information in the Program

#region QuestPDFset up
// 1、Please make sure you are eligible to use the community license, if you don't set it up, it will report an exception.
 = ;
// 2, disable the QuestPDF library in the text character availability checks
 = false;
#endregion

By default, when you use QuestPDF to generate a PDF document, it checks if the font used supports all characters in the text and outputs a warning message if it finds characters that cannot be displayed. This option ensures that all characters in the text are displayed correctly in the resulting PDF file.

2, generate the document controller code

/// <summary>
/// Create PDF files
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GeneratePdf")]
public IActionResult GeneratePdf()
{
    //Creating Documents
    var document = CreateDocument();
​
    // Generate PDF files
    var pdf = ();
​
    // Returns the file stream
    return File(pdf, "application/pdf", "");
}

3. Running effect

Call interface to generate PDF documents, download can be previewed, convenient and easy to write, you can go to try.

Some examples

text element

The text element is used to draw text with default or customized styles. Text always takes up the least amount of space possible.

If the text string is long, the element may take up the entire width and wrap to the next line. The element supports paging.

For most situations where complex formatting is not required, a simplified version of the text component will suffice, as shown below:

.Text("Sample text")
.Text("Red big text").FontColor("#F00").FontSize(24)

When you want to change the style in the middle of a text string, insert a page number, or include a custom component, use the "Text Block" method, as shown below:

.Text(text =>
{
    ("This is a normal text, followed by some ");
    ("underlined text.").Underline();
});

still image

element can be used to place an image within a document. By default, Image maintains the aspect ratio of the image. The image is loaded into an object.

Please note that all limitations are inherited from SkiaSharp. For example, the available image formats may vary from platform to platform.

Images in any common raster format can be used, such as JPG, PNG, BMP, and so on.

In C#, images can be provided in several ways:

byte[] imageData = ("path/to/");
(imageData);
​
//filename
("path/");
​
//file stream
using var stream = new ("", );
(stream);

The example shows how to load and use images in QuestPDF, and you can choose the most appropriate loading method according to your actual needs.

There are many more examples, so check out the documentation to learn and use them. More detailed tutorials and examples on how to use QuestPDF can be found through the official documentation.

The documentation not only contains basic usage instructions, but also provides demonstrations of advanced features.

Project Address

  • GitHub:/QuestPDF/QuestPDF
  • Documentation:/

summarize

I hope that this article for friends in the generation of PDF development to help. Welcome to leave a message in the comments section to discuss and share your experience and suggestions.

ultimate

If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.

You can also join WeChat[DotNet Technician] community to share ideas and grow with other tech-loving peers!Excellence is a habit, so feel free to leave a comment and learn!