PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Unable to get azure to work with Migra Docs
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4017
Page 1 of 1

Author:  alel3001 [ Thu Sep 19, 2019 8:37 pm ]
Post subject:  Unable to get azure to work with Migra Docs

I have been trying to get migradocs to generate a pdf on an azure serverless function for a few days now, and I have been unable to get it to work. I first ran into an error stating that I needed to write my own MyFontResolver class, and following some code guides I have assembled the following:

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

                // Deal with the fonts we know.
                switch (name)
                {
                    case "ubuntu":
                        if (isBold)
                        {
                            if (isItalic)
                                return new FontResolverInfo("Ubuntu#bi");
                            return new FontResolverInfo("Ubuntu#b");
                        }
                        if (isItalic)
                            return new FontResolverInfo("Ubuntu#i");
                        return new FontResolverInfo("Ubuntu#");

                    case "janitor":
                        return new FontResolverInfo("Janitor#");
                }

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

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

            public byte[] GetFont(string faceName)
            {
                switch (faceName)
                {
                    case "Arial#":
                        return LoadFontData("MyProject.fonts.arial.arial.ttf"); ;

                    case "Arial#b":
                        return LoadFontData("MyProject.fonts.arial.arialbd.ttf"); ;

                    case "Arial#i":
                        return LoadFontData("MyProject.fonts.arial.ariali.ttf");

                    case "Arial#bi":
                        return LoadFontData("MyProject.fonts.arial.arialbi.ttf");
                }

                return null;
            }
        }


Since then, I have set GlobalFontSettings.FontResolver = new MyFontResolver(); and also paragraph.Format.Font.Name = "ubuntu"; where paragraph is a MigraDoc.DocumentObjectModel.Paragraph that holds the text. The error I am now getting is:

Code:
System.NullReferenceException: Object reference not set to an instance of an object.
   at PdfSharp.Fonts.OpenType.OpenTypeFontface.CetOrCreateFrom(XFontSource fontSource)
   at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
   at PdfSharp.Drawing.XFont.Initialize()
   at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
   at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
   at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
   at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
   at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
   at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()


If I add paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50); Then I get a different error:

Code:
System.ArgumentNullException: Value cannot be null.
Parameter name: buffer
   at PdfSharp.Drawing.FontHelper.CalcChecksum(Byte[] buffer)
   at PdfSharp.Drawing.XFontSource.GetOrCreateFrom(Byte[] bytes)
   at PdfSharp.Fonts.FontFactory.ResolveTypeface(String familyName, FontResolvingOptions fontResolvingOptions, String typefaceKey)
   at PdfSharp.Drawing.XGlyphTypeface.GetOrCreateFrom(String familyName, FontResolvingOptions fontResolvingOptions)
   at PdfSharp.Drawing.XFont.Initialize()
   at MigraDoc.Rendering.FontHandler.FontToXFont(Font font, PdfFontEncoding encoding)
   at MigraDoc.Rendering.ParagraphRenderer.get_CurrentFont()
   at MigraDoc.Rendering.ParagraphRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
   at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
   at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
   at EmailQueue.AddCopyToEFolder.<Run>d__3.MoveNext()


I honestly just want to get a plain text document, so if there is an easier way to do this, I'm happy to listen

Author:  Thomas Hoevel [ Fri Sep 20, 2019 9:30 am ]
Post subject:  Re: Unable to get azure to work with Migra Docs

Already discussed on SO:
https://stackoverflow.com/a/57994361/162529

Author:  alel3001 [ Fri Sep 20, 2019 3:30 pm ]
Post subject:  Re: Unable to get azure to work with Migra Docs

I was able to get the pdf to render correctly now thanks. Is there a way to use Pdf sharp to save to a byte array as if I had used a File.ReadAllBytes(path) without saving using pdfDoc.save(path) and then using File.ReadAll Bytes(path)?

Author:  TH-Soft [ Sun Sep 22, 2019 10:29 pm ]
Post subject:  Re: Unable to get azure to work with Migra Docs

The best you can do is saving to a MemoryStream to avoid all file I/O.

Author:  Thomas Hoevel [ Mon Sep 23, 2019 10:56 am ]
Post subject:  Re: Unable to get azure to work with Migra Docs

alel3001 wrote:
I was able to get the pdf to render correctly now thanks.
It might help others here if you tell us how you got it to render correctly.
You did not accept an answer on SO.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/