Permalink
Browse files

Fix templating of pages with an inherited MediaBox entry

* thanks to takahashim for reporting
* fixes #138
  • Loading branch information...
1 parent 3b93a33 commit 41b56e8595cf10c7d6c63ce38c7aabfc0aef6942 @yob yob committed Aug 7, 2010
Showing with 29 additions and 1 deletion.
  1. +21 −1 lib/prawn/core/page.rb
  2. +8 −0 spec/template_spec.rb
View
@@ -130,7 +130,7 @@ def imported_page?
end
def dimensions
- return dictionary.data[:MediaBox] if imported_page?
+ return inherited_dictionary_value(:MediaBox) if imported_page?
coords = Prawn::Document::PageGeometry::SIZES[size] || size
[0,0] + case(layout)
@@ -173,6 +173,26 @@ def init_new_page(options)
@stamp_dictionary = nil
end
+ # some entries in the Page dict can be inherited from parent Pages dicts.
+ #
+ # Starting with the current page dict, this method will walk up the
+ # inheritance chain return the first value that is found for key
+ #
+ # inherited_dictionary_value(:MediaBox)
+ # => [ 0, 0, 595, 842 ]
+ #
+ def inherited_dictionary_value(key, local_dict = nil)
+ local_dict ||= dictionary.data
+
+ if local_dict.has_key?(key)
+ local_dict[key]
+ elsif local_dict.has_key?(:Parent)
+ inherited_dictionary_value(key, local_dict[:Parent].data)
+ else
+ nil
+ end
+ end
+
end
end
end
View
@@ -155,4 +155,12 @@
fonts.size.should == 2
end
+ it "should correctly import a template file that is missing a MediaBox entry" do
+ filename = "#{Prawn::BASEDIR}/data/pdfs/page_without_mediabox.pdf"
+
+ @pdf = Prawn::Document.new(:template => filename)
+ str = @pdf.render
+ str[0,4].should == "%PDF"
+ end
+
end

0 comments on commit 41b56e8

Please sign in to comment.