HiqPdf error - Cannot convert to PDF. Could not add demo watermark. Invalid font

1.3k Views Asked by At

HiqPdf - Everything works in my local with HiqPDF with 'Evaluation watermark', but when it is deployed in server(Azure), it throws error:

Error: Cannot convert to PDF. Could not add demo watermark. Invalid font.

I am sending html to convert to pdf.

Any help?

HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
                htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
                switch (htmlModel.PageOrientation)
                {
                    case Constants.PAGE_ORIENTATION_PORTRAIT:
                        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
                        break;
                    default:
                        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Landscape;
                        break;
                }
                htmlToPdfConverter.Document.Margins = new PdfMargins(10, 10, 10, 10);
                htmlToPdfConverter.SerialNumber = "";
                if (!string.IsNullOrEmpty(htmlModel.ElementSelector))
                {
                    htmlToPdfConverter.ConvertedHtmlElementSelector = htmlModel.ElementSelector;
                }
                string decodedHtml = htmlModel.HtmlString.Replace("$ReplaceSign1$", "<").Replace("$ReplaceSign2$", ">");
                htmlBytes = htmlToPdfConverter.ConvertHtmlToMemory(decodedHtml, HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority);
1

There are 1 best solutions below

0
keremispirli On

TL;DR:

Use Add-Font.ps1 from here: https://github.com/wormeyman/FindFonts to install the Microsoft Sans Serif font in C:\Windows\Fonts\micross.ttf into your Docker container or Windows Server.

If it doesn't work, HiqPdf may need a different font. Use Process Monitor from SysInternals to find out which one is needed.

Explanation

I got the same error when I tried to use Winnovative HTML-to-PDF in a Windows Server 2019 Docker container. The reason is that the docker base image for Windows Server doesn't contain the fonts Windows 10 contains and a specific font is needed for the conversion. In my case it was Microsoft Sans Serif. I used Process Monitor to find out which font(s) are needed.

Finding out needed font with Process Monitor

First use the "Filter" button to add a filter for "Process Name is fontdrvhost.exe":

Add filter for fontdrvhost.exe

Then capture the activity during PDF conversion. You'll see which fonts are accessed in the Path column:

Fonts accessed during conversion

Now we know which font we need.

Installing the font

Download Add-Font.ps1 from here: https://github.com/wormeyman/FindFonts and copy it to your server/container. I created a C:\fonts folder and copied the script into it for convenience.

Then copy the needed font into the same folder. In my case it was C:\Windows\Fonts\micross.ttf.

Run the script to install the font:

Add-Font.ps1 micross.tff

Now Html-PDF conversion should work properly.