Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException: 'Must not change font resolver after is was once used.' #1

Open
divyesh008 opened this issue Jan 5, 2022 · 1 comment

Comments

@divyesh008
Copy link

divyesh008 commented Jan 5, 2022

The print pdf button works fine for the first time but when again I am clicking Print pdf button the app is crashing. I have test this in UWP simulator.

Exception Details

System.InvalidOperationException
  HResult=0x80131509
  Message=Must not change font resolver after is was once used.
  Source=PdfSharpCore
  StackTrace:
   at PdfSharpCore.Fonts.GlobalFontSettings.set_FontResolver(IFontResolver value)
   at PDFDemo.PDFReports.ProductsReport..ctor(List`1 items) in D:\Demos\PDFDemo-master\PDFDemo-master\PDFDemo\PDFDemo\PDFReports\ProductsReport.cs:line 30
   at PDFDemo.ViewModels.ProductListViewModel.<PrintPdf>d__17.MoveNext() in D:\Demos\PDFDemo-master\PDFDemo-master\PDFDemo\PDFDemo\ViewModels\ProductListViewModel.cs:line 46
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PDFDemo.ViewModels.ProductListViewModel.<<-ctor>b__19_1>d.MoveNext() in D:\Demos\PDFDemo-master\PDFDemo-master\PDFDemo\PDFDemo\ViewModels\ProductListViewModel.cs:line 64

  This exception was originally thrown at this call stack:
    [External Code]
    PDFDemo.PDFReports.ProductsReport.ProductsReport(System.Collections.Generic.List<PDFDemo.Models.Product>) in ProductsReport.cs
    PDFDemo.ViewModels.ProductListViewModel.PrintPdf() in ProductListViewModel.cs
    [External Code]
    PDFDemo.ViewModels.ProductListViewModel..ctor.AnonymousMethod__19_1() in ProductListViewModel.cs
@JorgeTorresPadron
Copy link

JorgeTorresPadron commented Aug 4, 2022

I resolve this using a try-catch.

In this proyect he generates the pdfs using a command in "ProductListViewModel". The first time the user generates the pdf the fonts are loaded. The following times the user generates a pdf the fonts aren´t loaded so it doesn´t fail

private async Task PrintPdf()
{
   //this is the try-catch you have to add in ProductListViewModel
   try
   {
       GlobalFontSettings.FontResolver = new GenericFontResolver();

        var pdfReport = new PDFReports.ProductsReport(products);
        await pdfReport.CreateReport();
   }
   catch
   {
        var pdfReport = new PDFReports.ProductsReport(products);
        await pdfReport.CreateReport();
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants