0% found this document useful (0 votes)
12 views

How To Easily Create PDF Documents in ASP

The document discusses different ways to generate PDF documents in ASP.NET Core using libraries like QuestPDF and IronPDF. It covers creating PDFs from HTML templates, merging multiple PDF files, and returning PDFs from API endpoints. IronPDF allows converting HTML to PDF using templates while QuestPDF uses a fluent API to compose elements without HTML.

Uploaded by

Rony Olazabal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

How To Easily Create PDF Documents in ASP

The document discusses different ways to generate PDF documents in ASP.NET Core using libraries like QuestPDF and IronPDF. It covers creating PDFs from HTML templates, merging multiple PDF files, and returning PDFs from API endpoints. IronPDF allows converting HTML to PDF using templates while QuestPDF uses a fluent API to compose elements without HTML.

Uploaded by

Rony Olazabal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

How To Easily Create PDF Documents

in ASP.NET Core
Read on: milanjovanovic.tech

Read time: 5 minutes

Reporting is essential for business applications like e-commerce, shipping,


fintech, etc.
One of the most popular document formats for reporting purposes is PDF.
PDF stands for Portable Document Format. It's a file format to present
documents (including text formatting and images) independently of application
software, hardware, and operating systems.
Some common problems .NET developers will face when working with PDF
files:
 Creating dynamic PDF documents
 Designing a consistent page layout
 Customizing fonts on printed documents
Today I want to show you a few interesting ways to generate PDF files in .NET.

Creating PDF Files With QuestPDF


QuestPDF is an open-source .NET library for generating PDF documents. It
exposes a fluent API you can use to compose together many simple elements to
create complex documents. Unlike other libraries, it does not rely on HTML-to-
PDF conversion.
Let's install the QuestPDF NuGet package:

Here's how you can generate a simplified invoice with QuestPDF:


What I like about QuestPDF:
 Fluent API
 Easy to use
 Good documentation
What I don't like about QuestPDF:
 Having to write a lot of code to create documents
 Limited scope of features
 No HTML-to-PDF support
Licensing
QuestPDF is free for small companies and development use. There's also a
commercial license for larger companies. You can check out the licensing
details here.

HTML to PDF Conversion With IronPDF


The more common approach for generating PDF files is using an HTML
template.
My favorite library that supports this is IronPDF.
IronPDF is a C# PDF library that allows for fast and efficient manipulation of
PDF files. It also has many valuable features, like exporting to PDF/A
format and digitally signing PDF documents.
But what's the idea behind using an HTML template?
First of all, you have more control over formatting the document. You can use
CSS to style the HTML markup, which will be applied when exporting to a PDF
document.
An interesting implementation approach is using ASP.NET Core MVC
views and the Razor syntax. You can pass an object to the view at runtime to
render dynamic HTML content.
I've used this approach with MVC views on a few projects with excellent results.
Let's start by installing the IronPDF NuGet package:

I'm using a strongly typed Razor view to define my markup.


The InoviceViewModel class is the model, and it's used to create dynamic content.
Now, you need to use the IronPDF ChromePdfRenderer to convert the HTML to a
PDF document.

It really is that simple.


Licensing
IronPDF is free for development use and has multiple pricing tiers for
commercial use that you can check out here.

Merging Multiple PDF Files


Another common requirement I've seen is merging multiple PDF files. For
example, you could implement a feature to merge the monthly receipts for the
accounting department.
You can use the PdfDocument.Merge method to implement this. It accepts
a PdfDocument collection as the argument. You'll first have to load the PDF
documents into memory before merging them.
Here's an example:
Exporting PDF Files From an API
It's pretty straightforward to return a PDF file from an API endpoint in ASP.NET
Core.
Minimal APIs have the Results.File method accepting either a file path, stream,
or byte array. You also need to specify the content type and an optional file
name. The MIME type for PDF files is application/pdf.
Here's how you can return a PDF file from a byte array:

Takeaway
Choosing which PDF library you will use in .NET is an important consideration
to make. And while pricing is a significant factor, the features you want to use
will also dictate your choice.
QuestPDF is an excellent choice if you're looking for a (mostly) free option with
rich features. The library is constantly improved, and new features are being
added. However, it doesn't support HTML-to-PDF conversion and modifying
existing documents.
IronPDF is the library I've used most often on commercial projects. It has
fantastic features for working with PDF files, with many customization options.
The HTML-to-PDF conversion works like a charm.
The hardest part is picking the right tool for the job.
So I hope this is helpful.

You might also like