Pdf generation in .NET with DinkToPdf/SelectPdf + Azure Deployment and issues related to fonts loading in DinkToPdf

Ahmed Anwar
3 min readDec 6, 2023

--

Recently, I have worked on a project where an endpoint will receive some data in POST request and a single page pdf file will be generated accordingly.
But the issue was that there were some custom fonts and data should be printed with these custom fonts on the output pdf. Now how to solve it?

Now, after a thorough search on the internet, I found two libraries in .NET that allow me to easily(also cheaply) do it.
1. DinkToPdf — No limit on number of pages and totally free.
2. SelectPdf — There is a community edition that allows the generation of upto 5 pages. Additionally, SelectPdf has some deployment restrictions too. You can deploy it to Azure App Service but the plan should be basic.
A detailed guide is available at their site: https://selectpdf.com/html-to-pdf/docs/html/Deployment-Microsoft-Azure.htm

SelectPdf Deployment Guide

SelectPdf can load fonts from a url too e.g. You can use import url(link) in .css file or in style tag and it will load the fonts without any issue.

Now, DinkToPdf can load fonts(can’t load font if you have used a import url in .css file or in style tag) and you have to download the fonts files from the Google font website(or from other) and reference them in the src field.
For example:

font.css

As I have the .ttf fonts file (downloaded from the google fonts website) and I have used the exact font name in font-family field and since the .ttf files are in the same folder as the font.css flle so in src field, I have just specified the name of .ttf file.
In a template generator(which is a static class), I referenced the font.css file like this:
rootPath will come from _webHostEnvironment.WebRootPath:

string cssPath = @"file:///" + rootPath + @"\font.css";

Now on Azure App Service, the DinkToPdf will not be able to load fonts(I have tried almost all the solution available on the internet but nothing worked for me).
Since DinkToPdf has no page limitations and can be used in scenarios where there are more than 5pages need to be printed.
The only solution remaining is to use Virtual Machines and deploy your app there and use it as much as you can.
I personally created a Azure VM and hosted the app there and SelectPdf and DinkToPdf worked smoothly.

Copied from sd.blackball.lv

One thing that I have noted:

We have to load a .dll file to make DinkToPdf work smoothly and for it we make some changes in Program.cs(.NET 6).
I created two endpoints(one use SelectPdf and other one use DinkToPdf) in order to check which endpoint results are more better and SelectPdf endpoint generated Pdf lost some formatting, but DinkToPdf endpoint gives the correct pdf output.
When I commented the line that loads the .dll file(in Program.css), the SelectPdf endpoint generates the correct formatted pdf. Since the requirement was for one page so we just go for the SelectPdf.(it was client choice too)

Tutorials to set up the libs:

DinkToPdf: https://code-maze.com/create-pdf-dotnetcore/
SelectPdf: https://selectpdf.com/html-to-pdf/docs/html/ConvertHtmlCodeToPdf.htm

In next article, I will talk about how I set up the Azure VM and deploy the app from the Visual Studio 2022

--

--

Ahmed Anwar

Software Engineer @Trapeze Europe. I have keen interest in System Design and Data structure & Algorithm topics.