(616) 371-1037

[email protected]

Creating PDF Documents with Razor in ASP.NET Core 2.2 Web API

March 18, 2019 - Vladan Petrovic

No Comments

In this article, I will show you an easy way to create a PDF document and style it properly with Razor engine and the DinkToPdf library.

The source code of this project can be found on GitHub.

Setting up the project

For simplicity, we will create a default Web API project with default options that will have one endpoint that will be responsible for creating a PDF document and returning the created document to the user.

To start, let’s delete the existing controller and create our own controller that will be responsible for generating the PDF document. Let’s call it PDFController with one Action called CreatePdf. We will later modify this controller so it returns the PDF document.

Now let’s add the library that we will need for creating the PDF and manipulation of data.

Configuration of project

DinkToPDF

Add the DinkToPdf library. DinkToPDF is an awesome library that is a .NET wrapper around the popular wkhtmltopdf library and we will use it to convert an HTML template to a PDF document. Things that make this library great is that it can use styles as well, so we can have an HTML template and convert it to PDFDocument.

DinkToPDF on GitHub.

Let’s add this library through Package Manager Console ( we could easily add it through the NuGet package window, but I prefer PMC 😊 )

In order for DinkToPDF to work correctly, we will need to add some native PDF dlls libwkhtmltox and load them at start-up of the application. Those can be found on the source code of this project.

Razor Light

We will need to add Razor Light. Razor is a powerful library for templating HTML documents and has lots of features for rendering HTML content. Instead of pulling a big razor library we will use the light version of the Razor library since we will need only the rendering feature to convert that generated HTML content to a PDF document.

Data preparation

For simplicity, we will add simple DTO, let’s name it CardDTO with properties.

Next, let us add the static class that will mock our repository where will we pull entities and map them to DTOs to serve us data. We will call it CarRepository.cs

Razor HTML template

If you work with Razor you will know how powerful the Razor engine is, as it lets us write C# in HTML and performs server-side rendering of HTML. We will create a PDFTemplate.cshtml file under the Template folder. Our template will have a ViewModel CarDTO and it will have a simple way of displaying data.

With this, our configuration and preparation are done. Now we will wire everything up.

Wiring everything up

To wire everything up we will need to do a few things.

To load native PDF dlls we will need the CustomAssemblyLoadContext class that will be used to load those dlls (note: depending on the process you will require the 64bit or 32bit dlls)

And then in our Startup.cs in the method ConfigureServices add the following code for loading assemblies into memory

After this let’s add the Razor service

Additionally, let’s add a service that will be responsible for creating the PDF. Let’s call it IPDFService and it will have a single method Create

Now comes the fun part, creating the concrete implementation that will be used for heavy lifting with a proper injection of services that we prepared before.

As we can see the Create() method is where everything is happening. Let’s explain it a bit, we are getting data for the model from our mock of the repository class. This model will be used in our template for rendering using the Razor engine. Next, we are getting the template from our templates folder. Keep in mind we can have multiple templates and depending on the context we can load them independently. After we load the template we will pass the template with a model to razor engine.  And then we get the raw HTML content that we will pass to the DinkToPDF library to convert it to an array of bytes or PDF document. The one thing left to do is to wire this PDF service to our controller.

We will need to add this service to our Startup class so we have access to it across the application

So now in our PDFController, we will have something like this.

Now it is time to test it. If we start the application and go to the URL that is on the image, we should get a PDF document.

Wrapping up

Here I showed you a simple but yet powerful way of creating a dynamic PDF document. This solution can be used in many scenarios with a little adjustment. But with this simple example, you can see how Razor can be used as a templating engine. Happy coding! 🙂

 

Vladan Petrovic

Leave a comment

Your email address will not be published. Required fields are marked *