From cb47931f2bf6c4a7d9c40555530ffc965782296c Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Wed, 18 Oct 2017 21:26:31 +0700 Subject: [PATCH] Fix case where prawn-templates would cause PDF contents to disappear --- lib/pdf/core/page.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/pdf/core/page.rb b/lib/pdf/core/page.rb index 2c2838d..eca4078 100644 --- a/lib/pdf/core/page.rb +++ b/lib/pdf/core/page.rb @@ -22,6 +22,17 @@ def initialize(document, options = {}) end end + # If :Contents is a reference to an array, returns the resolved reference. + # Otherwise, makes sure :Contents is an array. + def ensure_contents_array + contents = dictionary.data[:Contents] + if contents.is_a?(PDF::Core::Reference) && contents.data.is_a?(Array) + return contents.data + end + # Ensure contents is an array. + dictionary.data[:Contents] = Array(contents) + end + # As per the PDF spec, each page can have multiple content streams. This # will add a fresh, empty content stream this the page, mainly for use in # loading template files. @@ -29,11 +40,9 @@ def initialize(document, options = {}) def new_content_stream return if in_stamp_stream? - unless dictionary.data[:Contents].is_a?(Array) - dictionary.data[:Contents] = [content] - end @content = document.ref({}) - dictionary.data[:Contents] << document.state.store[@content] + contents = ensure_contents_array + contents << document.state.store[@content] document.open_graphics_state end