PdfSharpCore
PdfSharpCore copied to clipboard
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Hello.
I use PdfSharpCore version 1.3.45 (.NET Core 5).
I use code below to generate a PDF-file:
public byte[] Execute()
{
using var document = new PdfDocument();
DrawReport(document);
using var stream = new MemoryStream();
document.Save(stream);
return stream.ToArray();
}
Sometimes I get an exception like this:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at PdfSharpCore.Fonts.OpenType.OpenTypeFontface.CreateFontSubSet(Dictionary2 glyphs, Boolean cidFont) at PdfSharpCore.Pdf.Advanced.PdfCIDFont.PrepareForSave() at PdfSharpCore.Pdf.Advanced.PdfType0Font.PrepareForSave() at PdfSharpCore.Pdf.Advanced.PdfFontTable.PrepareForSave() at PdfSharpCore.Pdf.PdfDocument.PrepareForSave() at PdfSharpCore.Pdf.PdfDocument.DoSave(PdfWriter writer) at PdfSharpCore.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream) at PdfSharpCore.Pdf.PdfDocument.Save(Stream stream) at iKassaShared.ReportService.Execute() in C:\Projects\iKassa\Receipts\iKassaShared\ReportService.cs:line 37 at ReceiptsCollector.SendingReportsJob.SendReport(List1 sendingReports, Boolean multipleForwarding) in C:\Projects\iKassa\Receipts\iKassaCollector\ReceiptsCollector\SendingReportsJob.cs:line 198
So it tries to call "CreateFontSubSet" method and falls with wrong index of array (???).
I am writing "sometimes" because the same code with same data can work all the day but sometimes it falls with the exception above.
Could you please look at this?
Thank you in advance.
I have tried to come around this error. And tried to call my method 5 times while will have no errors.
It's very strange. As result I have got a PDF-file with missing letters (just spaces instead of some letters).
And no exceptions in this case.
Actually it's a second issue and it's more critical issue.
I have investigated the issue. It looks like 2 simultaneous threads are trying to use the same resources in the same time. And they lock one another.
Are you able to provide a test case/console app that creates the issue?
@jafin
I encountered the same issue that @harry-flw described and was able to replicate it on a small size web app on this repo
https://github.com/AhmedDawood97/PdfSharpCoreBugShowCase
Please use it to see the problem first hand and let me know if it can be fixed.
You can run the unit test method created with the Run Until Failure option and it will fail at a certain point.
Should be fixed with commit dc02f37663096792f2e29b87be7716903bd4d44e