3

I try to get PdfSharp working in an Azure Function But I have some problems with fonts

 // Create a new PDF document
  PdfDocument document = new PdfDocument();
  document.Info.Title = "Created with PDFsharp";

  // Create an empty page
  PdfPage page = document.AddPage();

  // Get an XGraphics object for drawing
  XGraphics gfx = XGraphics.FromPdfPage(page);

  // Create a font
  XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);

  // Draw the text
  gfx.DrawString("Hello, World!", font, XBrushes.Black,
    new XRect(0, 0, page.Width, page.Height),
    XStringFormats.Center);

This is also the code from the PDFSharp sample page... At the font line he gives me the following error...

Exception while executing function: Functions.PDFGenerationFunction. PdfSharp: Internal error. Font data could not retrieved.

Do I need to reference something special? Or is it just not possible to do this in an Azure function?

PDFSharp version --> "PDFsharp" : "1.32.3057"

And/Or another solution to generate a PDF document in an Azure Function...

Sven
  • 885
  • 5
  • 11
  • 31
  • 1
    As answered elsewhere: Azure Functions (which run in the context of a Web App) is sandboxed, and you cannot install certain software (activex etc). – David Makogon Jul 18 '17 at 11:40

2 Answers2

2

This is likely an issue with the sandbox that Azure Functions and webapps run in. Check out this list for known PDF libraries that will work in the sandbox. https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks

Mike S
  • 3,058
  • 1
  • 22
  • 12
0

PDF Sharp is unable to find an appropriate font for the function and also for Web APIs/Apps. Because they are not provided with the standard fonts.

To resolve this one needs to code and provide a font resolver based on the PDFSharp interface IFontResolver to PDF Sharp. That resolver will actually acquire the font and return it as a stream.

The below example is a resolver I wrote to extract from the current assemblies embeded fonts such as:

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

The best example on how to create such a resolver on Stack Overflow is this answer (it is not my answer, but I have modified it to provide a better understanding of what happens)

Azure PDF Sharp not using Unicode font

If the function won't allow you to use embeded fonts, you can load them most likely from an Azure Blob.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122