SaveAsPdf problems

Posted by: martin.bruce on 10 March 2020, 12:05 am EST

  • Posted 10 March 2020, 12:05 am EST

    Hello,

    I'm generating a document and want to save it as a pdf. Locally this works fine but when running from a server I get the following error:

    TextFormat has no associated Font, and a default font could not be found. To avoid this situation, assign an available font to FontCollection.SystemFonts.DefaultFont if it is null.

    I asume the server doesn't have access to certain fonts so I followed what the error recomends by doing the following:



    _fontCollection = new FontCollection {DefaultFont = StandardFonts.Times};
    _fontCollection.RegisterDirectory("./Fonts");

    var font = _fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    _fontCollection.DefaultFont = font;
    }

    _logger.LogInformation($"Default font: {(_fontCollection.DefaultFont != null ? _fontCollection.DefaultFont.FontFamilyName : "not found.")} - Font count: {_fontCollection?.Count}");
    doc.SaveAsPdf(memoryStreamPdf, _fontCollection);


    I still get the same error but my logging logs the following:

    Default font: CorpoS - Font count: 4

    So what am I doing wrong here?
  • Replied 11 March 2020, 7:36 pm EST

    Hi Martin,
    We are sorry for the delayed response on this. We were trying to replicate the issue at our end using the provided code snippet by you assuming you are working with the Grapecity Documents for Word and generating the PDF file.
    Here, we observed that there is not FontCollection class available.

    Hence, could you please share a small demo sample depicting your issue. So that we can replicate the issue at our end and assist you accordingly.

    Regards,
    Manish Gupta
  • Replied 11 March 2020, 8:13 pm EST

    Hello Manish,

    Yes I'm using GrapeCity.Documents.Word and GrapeCity.Documents.Layout both version 3.0.0.42 to generate my documents.

    Please be advised that the issue doesn't arise running localy. It first apears once I have deployed on our test server. Is it posible that the issue arises from not having a license key on the test server?

    Below is a short example of what I do (I hope I got the structure right)):


    FontCollection _fontCollection = new FontCollection {DefaultFont = StandardFonts.Times};

    _fontCollection.RegisterDirectory("./Fonts");

    var font = _fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    _fontCollection.DefaultFont = font;
    }

    GcWordDocument doc = new GcWordDocument(_settings.GrapeCityKey);
    doc.Load(memoryStream); //memoryStream contains a docx templpate.

    var runs = doc.Body.Runs;
    List<PersistentRange> runRanges = new List<PersistentRange>(runs.Count);
    foreach (var run in runs)
    {
    runRanges.Add(run.GetPersistentRange());
    }

    foreach (var rr in runRanges)
    {
    //do a regex match
    if(match)
    {
    var font = rr.ParentRun.Font;
    rr.Clear();
    var r = rr.Runs.Last;

    foreach (Match m in matches)
    {
    r.GetRange().Runs.Insert(value, InsertLocation.After);
    r = SetFont(r, font); //Sets the font on the range above.
    }

    rr.Runs.First.Delete();
    }
    }

    var ms = new MemoryStream();
    doc.Save(ms, DocumentType.Document); //Word document is saved fine.

    var msPdf = new MemoryStream();

    //Error occurs on line below. Doesn't matter wich one I use.
    doc.SaveAsPdf(msPdf, _fontCollection); //doc.SaveAsPdf(msPdf);

  • Replied 12 March 2020, 9:01 pm EST

    Hi,
    Thank you for the provided code snippet. This issue requires further investigation hence we have reported this issue to the concerned team with internal tracking id DOC-1870.

    Regards,
    Manish Gupta
  • Marked as Answer

    Replied 17 March 2020, 6:10 am EST

    Hi Martin,

    Please replace the code at the top of your fragment with this:


    FontCollection.SystemFonts.DefaultFont = StandardFonts.Times;
    FontCollection fontCollection = new FontCollection();
    fontCollection.RegisterDirectory("./Fonts");
    var font = fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    FontCollection.SystemFonts.DefaultFont = font;
    }

    Let me know if this helps.

    Thanks,
    Dmitry.
  • Replied 17 March 2020, 6:15 pm EST

    Hello Dmitry

    That worked. I get my pdf's now.

    Thankyou!

    /Martin
  • Replied 18 March 2020, 1:06 am EST

    Hi Martin,

    Great, thanks for letting me know!

    Cheers,
    Dmitry.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels