Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure PDF Sharp not using Unicode font

We have a C# website that is hosted using Azure Cloud Services and uses PDF Sharp to generate PDF documents.

We are using the Arial Unicode MS Regular font since we have Japanese fonts to display. When the website is run locally (from Visual Studio on Windows 7) the fonts render correctly.

We use a start-up script for Azure to install the font onto the Cloud Service servers since its not installed by default. I have verified that the font is installed on the Cloud Service servers (Windows Server 2012).

In the Azure hosted website the Japanese fonts appear as squares, even though the PDF properties do indicate that the font used is Arial Unicode MS Regular.

Any ideas why the font is not being correctly used on the Cloud Service servers?

The PDF generated by Windows 2012 server running on Azure. The Japanese fonts rendered as sqaures but the properties indicate the unicode font was used.

like image 605
joechip Avatar asked Oct 02 '15 17:10

joechip


1 Answers

To solve this problem I cross-posted the question to the PDFSharp Forum Post and an answer pointed me in the right direction in creating a Font Resolver to be used:

  1. Install the WPF build of PDFsharp 1.50 beta 2 from NuGet
  2. Use a Font Resolver to select the font Using private fonts with PDFsharp 1.50 beta 2 or MigraDoc
  3. Add the unicode font as an embedded resource in the project.

I deployed this to Azure Cloud Services and confirmed that the unicode font was correctly used.


The documentation for PDFSharp is incomplete since it states that the GDI build is the correct one to use for .NET websites when in fact it is not in this case. Instead the WPF build using a FontResolver worked.


Example

Setup the FontResolver in Global.asax.cs :

PdfSharp.Fonts.GlobalFontSettings.FontResolver = new MyFontResolver();

Create a new class called MyFontResolver that extends the default implementation with handly additional font families which are included in embedded resources.

The font(s) themselves should be added to the font directory with build action = Embedded Resource.

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

        // Add fonts here
        switch (name)
        {
            case "arial unicode ms":
                return new FontResolverInfo("ArialUnicodeMS#");
        }

        //Return a default font if the font couldn't be found
        //this is not a unicode font 
        return PlatformFontResolver.ResolveTypeface("Arial", isBold, isItalic);
    }

    // Return the font data for the fonts.
    public byte[] GetFont(string faceName)
    {
        switch (faceName)
        {
            case "ArialUnicodeMS#": return FontHelper.ArialUnicodeMS; break;
        }

        return null;
    }
}

Helper class that reads font data from embedded resources.

public static class FontHelper
{
    public static byte[] ArialUnicodeMS
    {
        //the font is in the folder "/fonts" in the project
        get { return LoadFontData("MyApp.fonts.ARIALUNI.TTF"); }
    }

    /// Returns the specified font from an embedded resource.
    static byte[] LoadFontData(string name)
    {

        var assembly = Assembly.GetExecutingAssembly();

        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;
        }
    }
}

Define the font as usual when generating the PDF, for example:

 var style = document.Styles["Normal"];
 style.Font.Name = "Arial Unicode MS";
 style.Font.Size = 8;
like image 181
joechip Avatar answered Oct 11 '22 15:10

joechip