From fb153854dc9135738b5df0f0878ecd8b093fca55 Mon Sep 17 00:00:00 2001 From: Thomas Leitner Date: Fri, 18 Oct 2024 22:50:00 +0200 Subject: [PATCH] Fix Type::Page#contents to handle null values in /Contents array --- CHANGELOG.md | 1 + lib/hexapdf/type/page.rb | 2 +- test/hexapdf/type/test_page.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ec8909..8f4c15a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ that do not exist * [HexaPDF::Font::TrueTypeWrapper] to correctly handle the situation when multiple codepoints refer to the same glyph ID +* [HexaPDF::Type::Page#contents] to handle null values in /Contents array ## 0.47.0 - 2024-09-07 diff --git a/lib/hexapdf/type/page.rb b/lib/hexapdf/type/page.rb index 1b260ff8..b208dbca 100644 --- a/lib/hexapdf/type/page.rb +++ b/lib/hexapdf/type/page.rb @@ -358,7 +358,7 @@ def rotate(angle, flatten: false) def contents Array(self[:Contents]).each_with_object("".b) do |content_stream, content| content << " " unless content.empty? - content << content_stream.stream + content << content_stream.stream if content_stream.kind_of?(Stream) end end diff --git a/test/hexapdf/type/test_page.rb b/test/hexapdf/type/test_page.rb index c942d90c..56b5ef07 100644 --- a/test/hexapdf/type/test_page.rb +++ b/test/hexapdf/type/test_page.rb @@ -358,6 +358,12 @@ def reset_media_box page[:Contents] = [@doc.wrap({}, stream: 'q 10'), @doc.wrap({}, stream: 'w Q')] assert_equal('q 10 w Q', page.contents) end + + it "handles null objects in the /Contents array" do + page = @doc.pages.add + page[:Contents] = [@doc.wrap({}, stream: 'q 10'), nil] + assert_equal('q 10 ', page.contents) + end end describe "contents=" do