-
Notifications
You must be signed in to change notification settings - Fork 216
Description
Description
PDFSharp fails to open a document, created by PDFSharp when document contains a JPEG image made from a MemoryStream. I've tested bmp and png files and the bug seems to not affect them, but don't quote me on that.
I've reproduced this bug both on Linux and Windows.
The bug is not reproduced with FileStream and w/e stream ASP.NET Core wraps Request's body with.
The document can be opened by means of other PDF viewers (Firefox, Adobe Acrobat).
Steps to reproduce
var path = @""; // your path to jepg
using var fs = File.OpenRead(path);
using var ms = new MemoryStream();
fs.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin); // not required
using var image = XImage.FromStream(ms); // if you pass fs stream directly without copying to Memory - no error raised on open
using var document = new PdfDocument();
var page = document.AddPage();
double width = Math.Min(595, image.PixelHeight);
double height = width * image.PixelHeight / image.PixelWidth;
page.Width = XUnit.FromPoint(width);
page.Height = XUnit.FromPoint(height);
using var gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(image, 0, 0, width, height);
// Doesn't matter if you save to file or stream. For this example I'm saving to file
string outPath = $"broken.pdf";
await document.SaveAsync(outPath);
using var savedDocumentFs = File.OpenRead(outPath);
using var pdf = PdfReader.Open(savedDocumentFs); // this line throws if JPEG in memory stream was passed to original documentExpected result
PDF file opens
Actual result
Exception ObjectNotAvailableException is being thrown with message Cannot retrieve stream length. PDFsharp cannot read this PDF file. If you think your file is a valid PDF file please send it to us so that we can fix this bug in the PDF parser.
Stack trace:
at PdfSharp.Pdf.IO.Lexer.<>c.<DetermineStreamLength>b__17_0() in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Lexer.cs:line 980
at PdfSharp.Internal.SuppressExceptions.HandleError(SuppressExceptions suppressExceptions, Action throwException) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Internal\SuppressExceptions.cs:line 25
at PdfSharp.Pdf.IO.Lexer.DetermineStreamLength(Int64 start, Int32 searchLength, SuppressExceptions suppressObjectOrderExceptions) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Lexer.cs:line 980
at PdfSharp.Pdf.IO.Parser.ReadDictionaryStream(PdfDictionary dict, SuppressExceptions suppressObjectOrderExceptions) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Parser.cs:line 394
at PdfSharp.Pdf.IO.Parser.ReadObjectInternal(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjectStream, SuppressExceptions suppressObjectOrderExceptions) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Parser.cs:line 329
at PdfSharp.Pdf.IO.Parser.ReadIndirectObject(PdfReference pdfReference, SuppressExceptions suppressObjectOrderExceptions, Boolean withoutDecrypting) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Parser.cs:line 946
at PdfSharp.Pdf.IO.Parser.ReadAllIndirectObjects() in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\Parser.cs:line 1062
at PdfSharp.Pdf.IO.PdfReader.OpenFromStream(Stream stream, String password, PdfDocumentOpenMode openMode, PdfPasswordProvider passwordProvider, PdfReaderOptions options) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:line 392
at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openMode, PdfPasswordProvider passwordProvider, PdfReaderOptions options) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:line 238
at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, PdfDocumentOpenMode openMode, PdfReaderOptions options) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:line 215
at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream) in D:\THHO\Repos\PDFsharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:line 209
(!works) jesterrace.jpeg
(works) jesterrace.png
(works) jesterrace.bmp