CleverPatrick
CleverPatrick

Reputation: 9483

PdfSharp and MigraDoc Rendering Font with no spacing

I am generating a PDF using a combination of PdfSharp and MigraDoc features. This generator is hosted in an ASP.NET MVC application.

In this test code, I am rendering 5 different fonts based on their name. The first one, "House M.D." exists only on my machine and does NOT exist on the server. I am using it as a control.

The next three fonts (the Helvetica variations) are the ones I care about and want to use in my actual output.

The last font ("Garbage - this is not a real font name") is a font that does not exist on the server OR on my machine. I just made it up. It, too, is a control.

All fonts are TTF fonts.

When I run on my local machine (whether hosted in the development webserver or in IIS), I get the expected results:

enter image description here

When I run the exact same code from the server, I get a completely different result, the wrong font is used, and the spacing between words completely goes away:

enter image description here

Here is the code I am using to generate the PDF (note, this is just test code in a test project I created to replicate the issue):

    public byte[] GeneratePdf()
    {
        var pdfOut = new PdfDocument();
        pdfOut.Info.Title = "Test Pdf";
        pdfOut.AddPage();


        using (var xgraphics = XGraphics.FromPdfPage(pdfOut.Pages[0]))
        {
            xgraphics.MFEH = PdfFontEmbedding.Always;

            var migraDoc = new Document();

            Section section = migraDoc.AddSection();
            var table = section.AddTable();
            table.AddColumn(new Unit(pdfOut.Pages[0].Width.Point/2));

            var fonts = new List<string>()
                        {
                            "House M.D.",
                            "HelveticaLTPro-Condensed",
                            "HelveticaLTPro-BoldCond",
                            "HelveticaLTPro-CondensedObl",
                            "Garbage - this is not a real font name"
                        };

            foreach (string fontName in fonts)
            {

                var mfont = new Font(fontName, 8);
                Row bodyRow = table.AddRow();
                var r3 = bodyRow.Cells[0].AddParagraph();
                var ft = r3.AddFormattedText(fontName, mfont);
                if (fontName.Contains("Bold")) ft.Bold = true;
            }

            var renderer = new DocumentRenderer(migraDoc);
            renderer.PrepareDocument();

            renderer.RenderObject(xgraphics, 10, 100, pdfOut.Pages[0].Width.Point, table);

            var ms = new MemoryStream();
            pdfOut.Save(ms);

            return ms.ToArray();

        }
    }

and, in case it matters, here is the WebApi controller that calls the above code:

[RoutePrefix("api/preview")]
public class PdfController : ApiController
{

    [Route("")]
    //[CacheOutput(ClientTimeSpan = 14400, ServerTimeSpan = 14400)]
    public HttpResponseMessage Get()
    {
        var generator = new PdfGenerator();
        byte[] pdfBytes = generator.GeneratePdf();
        var ms = new MemoryStream(pdfBytes);
        var response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(ms);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        return response;
    }
}

What am I doing wrong? Why can't I get these fonts to render on the server?

Upvotes: 1

Views: 885

Answers (1)

CleverPatrick
CleverPatrick

Reputation: 9483

So, this is an "answer" but it isn't a very good one.

I just rebooted the machine (I hadn't done that since installing the fonts) and now it works.

I had stopped and started IIS, done an IISRESET, and various other things previously. But had not rebooted.

I just rebooted and it worked.

Upvotes: 2

Related Questions