6

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.

cyptus
  • 3,346
  • 3
  • 31
  • 52
  • Have you tried running wkhtmltopdf locally?. I register fonts in this way `src: url("../fonts/open-sans.ttf") format("truetype");` and it works for me – hardkoded Jul 15 '17 at 15:20
  • What do you mean with locally? Locally on my PC ASP.net App ist running fine. – cyptus Jul 15 '17 at 19:22
  • Do you run your app in azure as webapp ? Do you also use the nreco wrapper? – cyptus Jul 15 '17 at 19:24
  • Running wkhtmltopdf.exe in your PC, maybe you think it's azure or nreco and it's not that. – hardkoded Jul 15 '17 at 19:44
  • 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 Jul 15 '17 at 22:13
  • i've tried your code and my text block with this font is not rendered at all, do you have any ideas? any azure app settings? – cyptus Jul 16 '17 at 07:32
  • when doing the same with an woff instead of an ttf file, the text block is rendered. but with the default font, not the loaded font – cyptus Jul 16 '17 at 07:41
  • I'm running it in a VM not in a WebApp I didn't even know that you would be able to run an exe in an Azure WebApp – hardkoded Jul 16 '17 at 14:14
  • Okay thats why.. VM should work fine, i am stuggeling with the azure web apps – cyptus Jul 16 '17 at 16:26
  • Did you find any solution? – Jeeva J Mar 17 '21 at 05:33
  • Kind of: you can use container instances where you can install fonts – cyptus Mar 17 '21 at 06:11

3 Answers3

4

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.

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • Thanks. So why are these GDI API limitations effects custom fonts and not default fonts ? Is there any list which fonts are available in an azure web App Environment? I could not find one. – cyptus Jul 16 '17 at 19:46
  • 1
    @cyptus wkhtmltopdf cannot load external font file because Azure Apps sandbox doesn't allow that (at least for now) and it fallbacks to the font that is already present. I guess all fonts that are usually present on Windows Server could be used. – Vitaliy Fedorchenko Jul 17 '17 at 07:23
  • while azure apps sandbox blocks external font loading, there can't be any other wrapper (like rotativa or sql reporting framework) which can render custom fonts, right? – cyptus Jul 17 '17 at 07:40
  • all WebKit/Qt-based solutions (wkhtmltopdf, phantomjs) use the same HTML rendering engine and limitations also the same. I don't know how SQL reporting framework produces PDF, possibly they use another approach. I hope Azure team will change sandbox settings to allow custom fonts usage in the future. – Vitaliy Fedorchenko Jul 17 '17 at 08:02
  • thank you - this seems to be a valid list for available fonts: https://www.microsoft.com/typography/fonts/product.aspx?PID=160 – cyptus Jul 17 '17 at 11:02
  • I thought to use the custom fonts in azure with linux. Is that ok to use? – Jeeva J Feb 02 '21 at 14:28
1

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.

Community
  • 1
  • 1
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • thank you - how can rotativa (based on wkhtmltopdf) be supported while nreco (based on wkhtmltopdf) is not supported? aren't these both are "just" .net wrappers? – cyptus Jul 17 '17 at 07:23
  • 1
    Tom, this is definitely 'inaccurate' official document (last edit May 2015 - it is outdated actually); all HTML-to-PDF components that based on wkhtmltopdf will work identically (and with the same custom fonts usage limitation). I have no idea why NReco wrapper is placed into "unsupported" list. – Vitaliy Fedorchenko Jul 17 '17 at 07:28
  • Rotativa doesn't work with custom fonts either in Basic plan of Azure App Service. A blanket statement that it works is incorrect. There are limitations, e.g. no use of fonts that are not installed on the host. – Manfred Oct 26 '17 at 06:16
1

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

Darius Kucinskas
  • 10,193
  • 12
  • 57
  • 79