5
votes

i am converting an html file to an pdf with the .net wrapper "NReco.PdfGenerator" for wkhtmltopdf on an azure web app (running in standard plan) but cannot include custom fonts in my document. The custom fonts are not loaded by the PDF generator or just ignored from it. I've tried different formats for including the font files like link to google fonts css, import from google fonts, relativ local paths, absolute local paths, loading the font file over localhost and font-faces with base64 encoded fonts:

@@font-face {
            font-family: 'testfont';
            src:  url(data:application/x-font-woff;charset=utf-8;base64,...) format('woff');
            font-weight: normal;
            font-style: normal;
        }

using with:

font-family: 'testfont';

@@ because i am using razor views. When saving the generated HTML to file it looks fine and works fine when opening in browser (chrome).

Generating the PDF:

var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
return htmlToPdf.GeneratePdf(html);

I also removed backup fonts from the font-family statement. I've tried different versions from wkhtmltopdf without luck. The generator handels images from localhost fine.

When running the same asp.net app on my windows machine the fonts works fine.

The NReco docs says "avoid usage of custom fonts" (in azure web app enviroment), does this mean there is no chance to get it running? Why are custom fonts not supported? Is there any workaround?

thanks for any help.

3
Have you tried running wkhtmltopdf locally?. I register fonts in this way src: url("../fonts/open-sans.ttf") format("truetype"); and it works for mehardkoded
What do you mean with locally? Locally on my PC ASP.net App ist running fine.cyptus
Do you run your app in azure as webapp ? Do you also use the nreco wrapper?cyptus
Running wkhtmltopdf.exe in your PC, maybe you think it's azure or nreco and it's not that.hardkoded
the wkhtmltopdf itself ist running good, it generates my PDFs, but without the custom Fonts when running in azure. Is your tool running in azure with nreco and the Fonts are rendered?cyptus

3 Answers

4
votes

This is known issue: Azure Apps hosting environment restricts some GDI APIs and as result wkhtmltopdf cannot load (and render) custom fonts. Only standard fonts that are present on Windows by default could be used (like Arial, Times New Roman etc). You may contact Azure Support on this topic - they could provide more details on the Azure Apps sandbox limitations.

1
votes

Why are custom fonts not supported? Is there any workaround?

According to Azure official document, we could know that NReco (uses wkhtmltopdf) is not supported on the Azure WebApp currently. The following is snippet from the document.

PDF generation from HTML

There are multiple libraries used to convert HTML to PDF. Many Windows/.NET specific versions leverage IE APIs and therefore leverage User32/GDI32 extensively. These APIs are largely blocked in the sandbox (regardless of plan) and therefore these frameworks do not work in the sandbox.

There are some frameworks that do not leverage User32/GDI32 extensively (wkhtmltopdf, for example) and we are working on enabling these in Basic+ the same way we enabled SQL Reporting.

Unsupported frameworks

Here is a list of frameworks and scenarios that have been found to be not be usable due to one or more of the restrictions above. It's conceivable that some will be supported in the future as the sandbox evolves.

PDF generators failing due to restriction mentioned above:

  • Syncfusion
  • Siberix
  • NReco (uses wkhtmltopdf)
  • Spire.PDF

If Nreco is not the only choice. Please have a try to use the following way:

The following PDF generators are supported:

  • SQL Reporting framework: requires the site to run in Basic or higher (not that this currently does not work Functions apps)

  • EVOPDF: See http://www.evopdf.com/azure-html-to-pdf-converter.aspx for vendor solution

  • Telerik reporting: requires the site to run in Basic or higher.

  • Rotativa / wkhtmltopdf: requires the site to run in Basic or higher.

1
votes

To overcome azure web app sandbox restrictions (custom fonts rendering) you may use Windows Containers in App Service to apply custom fonts: https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-windows-containers-custom-fonts