From a2e6775dd8bc0d988e6fa999e5022862903bfb1f Mon Sep 17 00:00:00 2001 From: Nicola Verbeeck Date: Sun, 7 Jan 2024 22:17:48 +0100 Subject: [PATCH] Allow parsing of streams with extra whitespaces (#51) Fixes #50 --- CHANGELOG.md | 4 ++++ lib/src/parser/pdf_object_parser.dart | 4 ++-- pubspec.yaml | 4 ++-- test/parser/pdf_stream_parser.dart | 26 ++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed12333..3a13c4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.1 + +- Fix parsing PDFs where stream or startxref ends with extra whitespace (#50) + ## 0.5.0 - Add support for web by using the archive package. (#48) diff --git a/lib/src/parser/pdf_object_parser.dart b/lib/src/parser/pdf_object_parser.dart index a1ada68..fb676cd 100644 --- a/lib/src/parser/pdf_object_parser.dart +++ b/lib/src/parser/pdf_object_parser.dart @@ -254,8 +254,8 @@ class PDFObjectParser { } Future _parseStream(PDFDictionary dictionary) async { - final line = await ReaderHelper.readLine(_buffer); - if ('tartxref' == line) return null; + final line = (await ReaderHelper.readLine(_buffer))?.trim(); + if (line == 'tartxref') return null; assert('tream' == line); var object = dictionary[PDFNames.length]; diff --git a/pubspec.yaml b/pubspec.yaml index fc19f83..ac7fabe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dart_pdf_reader description: Small pure-dart library for parsing PDF files. Supports reading all pdf structures -version: 0.5.0 +version: 0.5.1 repository: https://github.com/NicolaVerbeeck/dart_pdf_reader issue_tracker: https://github.com/NicolaVerbeeck/dart_pdf_reader/issues topics: [pdf, parser] @@ -17,6 +17,6 @@ dependencies: dev_dependencies: lints: ">=2.0.0 <4.0.0" test: ^1.21.0 - dcli: ^3.0.2 + dcli: ^3.2.4 mocktail: ^1.0.0 convert: ^3.1.1 diff --git a/test/parser/pdf_stream_parser.dart b/test/parser/pdf_stream_parser.dart index 95fc913..5908247 100644 --- a/test/parser/pdf_stream_parser.dart +++ b/test/parser/pdf_stream_parser.dart @@ -52,5 +52,31 @@ void streamParserTests() { final raw = await stream.readRaw(); expect(raw, [97, 98, 99]); }); + test('Test stream extra space, with data', () async { + final stream = + await createParserFromString('<>stream \nabcendstream') + .parse(); + expect(stream, isA()); + + stream as PDFStreamObject; + expect(stream.dictionary.entries, { + const PDFName('Length'): const PDFNumber(3), + }); + expect(stream.length, 3); + expect(stream.offset, 21); + final raw = await stream.readRaw(); + expect(raw, [97, 98, 99]); + }); + test('Test startxref keyword having trailing whitespace', () async { + final stream = + await createParserFromString('<>startxref \nabcendstream') + .parse(); + expect(stream, isA()); + + stream as PDFDictionary; + expect(stream.entries, { + const PDFName('Length'): const PDFNumber(3), + }); + }); }); }