PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 7:46 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Sep 28, 2020 10:08 am 
Offline

Joined: Mon Sep 28, 2020 8:33 am
Posts: 5
I am trying to use PDFSharp in an Azure Function to produce a sales proposal document. The marketing department want me to use the Montserrat font to write to the document. I discovered that I need to use a FontResolver to enable this. I have added the font to the embedded resources of the project and got the resource name using JetBrains dotPeek. I have implemented a Resolver Class as below and I instantiate the class using

GlobalFontSettings.FontResolver = new MyFontResolver();

The local Azure emulator on my laptop works without issue, however as soon as I publish to Azure I get the following error when I try to call the function.

Microsoft Azure returns STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) from GetFontData. This is a bug in Azure. You must implement a FontResolver to circumvent this issue.

Am I missing something here?


class MyFontResolver : IFontResolver
{
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
// Ignore case of font names.
var name = familyName.ToLower().TrimEnd('#');

// Deal with the fonts we know.
switch (name)
{

case "montserrat":
if (isBold)
{
if (isItalic)
return new FontResolverInfo("Montserrat#");
return new FontResolverInfo("Montserrat#");
}
if (isItalic)
return new FontResolverInfo("Montserrat#");
return new FontResolverInfo("Montserrat#");
}

// We pass all other font requests to the default handler.
// When running on a web server without sufficient permission, you can return a default font at this stage.
return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic);
}

public byte[] GetFont(string faceName)
{
switch (faceName)
{
case "Montserrat#":
return LoadFontData("CreateProposalDocument.Resources.Montserrat-Regular.ttf");

}

return null;
}

/// <summary>
/// Returns the specified font from an embedded resource.
/// </summary>
private byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();

// Test code to find the names of embedded fonts - put a watch on "ourResources"
//var ourResources = assembly.GetManifestResourceNames();

using (Stream stream = assembly.GetManifestResourceStream(name))
{
if (stream == null)
throw new ArgumentException("No resource with name " + name);

int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);
return data;
}
}

internal static MyFontResolver OurGlobalFontResolver = null;

/// <summary>
/// Ensure the font resolver is only applied once (or an exception is thrown)
/// </summary>
internal static void Apply()
{
if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null)
{
if (OurGlobalFontResolver == null)
OurGlobalFontResolver = new MyFontResolver();

GlobalFontSettings.FontResolver = OurGlobalFontResolver;
}
}
}


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 28, 2020 1:24 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
Set a breakpoint on "PlatformFontResolver.ResolveTypeface" and make sure this line does not get called, neither locally nor on Azure.
It'll work locally, but not on Azure.
Maybe throw an exception instead of calling "PlatformFontResolver.ResolveTypeface".

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 28, 2020 3:41 pm 
Offline

Joined: Mon Sep 28, 2020 8:33 am
Posts: 5
Aha, I discovered that further down I was calling a font that was not included in the Font Resolver.

All fixed and working. Thank you.


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 02, 2022 6:38 am 
Offline

Joined: Sun Oct 02, 2022 6:32 am
Posts: 2
Hi,
When I published asp.net core web api to azure app service and using PDFSharp, I am getting below error

"Microsoft Azure returns STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L) from GetFontData. This is a bug in Azure. You must implement a FontResolver to circumvent this issue."

you have provided solution on
viewtopic.php?f=2&t=4179

But I did not understand when and where to call

GlobalFontSettings.FontResolver = new MyFontResolver();

Please help.


/***************************/
I found an error in my code , I forget to register resolver
GlobalFontSettings.FontResolver = new MyFontResolver();

That means we need to register all fonts that we have used to create a PDF file ? Is this correct?

But I have used PdfSharpCore since I am using .net core 6.0 version.

Thanks.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 52 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group