Skip to content

Commit 223f5ef

Browse files
committed
Fix case where prawn-templates would cause PDF contents to disappear
1 parent ec2d24d commit 223f5ef

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/pdf/core/page.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,30 @@ def initialize(document, options = {})
2222
end
2323
end
2424

25+
# If :Contents is a reference to an array, returns the resolved reference.
26+
# Otherwise, makes sure :Contents is an array.
27+
def ensure_contents_array
28+
contents = dictionary.data[:Contents]
29+
if contents.is_a?(PDF::Core::Reference) && contents.data.is_a?(Array)
30+
contents = contents.data
31+
else
32+
# Ensure contents is an array.
33+
contents = Array(contents)
34+
dictionary.data[:Contents] = contents
35+
end
36+
contents
37+
end
38+
2539
# As per the PDF spec, each page can have multiple content streams. This
2640
# will add a fresh, empty content stream this the page, mainly for use in
2741
# loading template files.
2842
#
2943
def new_content_stream
3044
return if in_stamp_stream?
3145

32-
unless dictionary.data[:Contents].is_a?(Array)
33-
dictionary.data[:Contents] = [content]
34-
end
3546
@content = document.ref({})
36-
dictionary.data[:Contents] << document.state.store[@content]
47+
contents = ensure_contents_array
48+
contents << document.state.store[@content]
3749
document.open_graphics_state
3850
end
3951

0 commit comments

Comments
 (0)