ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.06k stars 235 forks source link

System.IndexOutOfRangeException: Index was outside the bounds of the array. #328

Closed harry-flw closed 2 months ago

harry-flw commented 1 year ago

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.

harry-flw commented 1 year ago

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.

harry-flw commented 1 year ago

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.

jafin commented 1 year ago

Are you able to provide a test case/console app that creates the issue?

AhmedDawood97 commented 11 months ago

@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. image

ststeiger commented 2 months ago

Should be fixed with commit dc02f37663096792f2e29b87be7716903bd4d44e