Guest User

Untitled

a guest
May 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. ## examples/general/repeating_elements.rb
  2. # encoding: utf-8
  3. #
  4. # This demonstrates repeating elements.
  5. #
  6. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
  7. require "prawn"
  8.  
  9. Prawn::Document.generate("repeating_elements.pdf") do
  10. repeating_elements(:all) do
  11. text "A page"
  12. end
  13.  
  14. repeating_elements(2..4) do
  15. text "Page 2, 3 or 4"
  16. end
  17.  
  18. repeating_elements(:even) do
  19. text "An even page"
  20. end
  21.  
  22. repeating_elements(:odd) do
  23. text "An odd page"
  24. end
  25.  
  26. repeating_elements([2, 4]) do
  27. text "Page 2 or 4"
  28. end
  29.  
  30. 5.times { start_new_page }
  31. end
  32.  
  33. ## Patch [diff]
  34. diff --git a/lib/prawn/document.rb b/lib/prawn/document.rb
  35. index 5ef95b1..2f19777 100644
  36. --- a/lib/prawn/document.rb
  37. +++ b/lib/prawn/document.rb
  38. @@ -158,6 +158,13 @@ module Prawn
  39. @pages.data[:Count]
  40. end
  41.  
  42. + # Repeats an element across multiple pages. 'spec' can be :all, :even,
  43. + # :odd, a range or an array.
  44. + def repeating_elements(spec = :all, &block)
  45. + @repeating_elements ||= {}
  46. + @repeating_elements[spec] = block
  47. + end
  48. +
  49. # The current y drawing position relative to the innermost bounding box,
  50. # or to the page margins at the top level.
  51. #
  52. diff --git a/lib/prawn/document/internals.rb b/lib/prawn/document/internals.rb
  53. index 086078a..b150d02 100644
  54. --- a/lib/prawn/document/internals.rb
  55. +++ b/lib/prawn/document/internals.rb
  56. @@ -71,13 +71,27 @@ module Prawn
  57.  
  58. private
  59.  
  60. - def finish_page_content
  61. - @header.draw if @header
  62. - @footer.draw if @footer
  63. + def finish_page_content
  64. + render_repeating_elements if @repeating_elements
  65. add_content "Q"
  66. @page_content.compress_stream if compression_enabled?
  67. @page_content.data[:Length] = @page_content.stream.size
  68. end
  69. +
  70. + def render_repeating_elements
  71. + @repeating_elements.each do |spec, proc|
  72. + case spec
  73. + when :all
  74. + proc.call(self)
  75. + when :even
  76. + proc.call(self) if page_count % 2 == 0
  77. + when :odd
  78. + proc.call(self) if page_count % 2 != 0
  79. + when Range, Array
  80. + proc.call(self) if spec.include?(page_count)
  81. + end
  82. + end
  83. + end
  84.  
  85. # Write out the PDF Header, as per spec 3.4.1
  86. def render_header(output)
  87. diff --git a/spec/document_spec.rb b/spec/document_spec.rb
  88. index 11b91d7..bcb2cb8 100644
  89. --- a/spec/document_spec.rb
  90. +++ b/spec/document_spec.rb
  91. @@ -45,6 +45,68 @@ describe "When creating multi-page documents" do
  92.  
  93. end
  94.  
  95. +describe "The repeating_elements() feature" do
  96. + before(:each) { @pdf = Prawn::Document.new }
  97. +
  98. + it "should render the element on all pages with :all" do
  99. + @pdf.repeating_elements(:all) {|p| p.text "This is rendered on all pages" }
  100. + 4.times { @pdf.start_new_page }
  101. +
  102. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  103. + strings.size.should == 5
  104. + strings.uniq.size.should == 1
  105. + end
  106. +
  107. + it "should render the element on even pages with :even" do
  108. + @pdf.repeating_elements(:even) {|p| p.text "This is rendered on even pages" }
  109. +
  110. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  111. + strings.size.should == 0
  112. +
  113. + 3.times { @pdf.start_new_page }
  114. +
  115. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  116. + strings.size.should == 2
  117. + end
  118. +
  119. + it "should render the element on odd pages with :odd" do
  120. + @pdf.repeating_elements(:odd) {|p| p.text "This is rendered on odd pages" }
  121. +
  122. + @pdf.start_new_page
  123. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  124. + strings.size.should == 1
  125. +
  126. + 2.times { @pdf.start_new_page }
  127. +
  128. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  129. + strings.size.should == 2
  130. + end
  131. +
  132. + it "should render the element on pages within the range with a Range" do
  133. + @pdf.repeating_elements(2..4) {|p| p.text "This is rendered on page 2 through 4" }
  134. +
  135. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  136. + strings.size.should == 0
  137. +
  138. + 10.times { @pdf.start_new_page }
  139. +
  140. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  141. + strings.size.should == 3
  142. + end
  143. +
  144. + it "should render the element on pages in the array whith an Array" do
  145. + @pdf.repeating_elements([2, 4]) {|p| p.text "This is rendered on page 2 and 4" }
  146. +
  147. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  148. + strings.size.should == 0
  149. +
  150. + 10.times { @pdf.start_new_page }
  151. +
  152. + strings = PDF::Inspector::Text.analyze(@pdf.render).strings
  153. + strings.size.should == 2
  154. + end
  155. +end
  156. +
  157. describe "When beginning each new page" do
  158.  
  159. describe "Background template feature" do
Add Comment
Please, Sign In to add comment