custom fonts with wkhtmltopdf wrapper nreco on azure web app
Asked Answered
C

3

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.

Custommade answered 15/7, 2017 at 13:8 Comment(11)
Have you tried running wkhtmltopdf locally?. I register fonts in this way src: url("../fonts/open-sans.ttf") format("truetype"); and it works for meRanie
What do you mean with locally? Locally on my PC ASP.net App ist running fine.Custommade
Do you run your app in azure as webapp ? Do you also use the nreco wrapper?Custommade
Running wkhtmltopdf.exe in your PC, maybe you think it's azure or nreco and it's not that.Ranie
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?Custommade
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?Custommade
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 fontCustommade
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 WebAppRanie
Okay thats why.. VM should work fine, i am stuggeling with the azure web appsCustommade
Did you find any solution?Viva
Kind of: you can use container instances where you can install fontsCustommade
A
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.

Aleida answered 16/7, 2017 at 16:30 Comment(6)
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.Custommade
@Custommade 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.Aleida
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?Custommade
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.Aleida
thank you - this seems to be a valid list for available fonts: microsoft.com/typography/fonts/product.aspx?PID=160Custommade
I thought to use the custom fonts in azure with linux. Is that ok to use?Viva
A
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.

Alfrediaalfredo answered 17/7, 2017 at 3:3 Comment(3)
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?Custommade
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.Aleida
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.Whisky
L
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

Lorsung answered 7/5, 2019 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.