Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-page text overlaps repeating header #1160

Open
tbcooney opened this issue Jun 8, 2020 · 3 comments
Open

Multi-page text overlaps repeating header #1160

tbcooney opened this issue Jun 8, 2020 · 3 comments

Comments

@tbcooney
Copy link

tbcooney commented Jun 8, 2020

I have a repeat(:all) action to act as a header on every page. However, the main content I am printing out spans across multiple pages and text will flow to the next page overlapping the header. How can I get Prawn to respect the header? move_down 20 doesn't work for the subsequent pages.

require 'prawn'
require 'prawn/table'

module Lib
  module Documents
    class SignableDocument < Prawn::Document

      def initialize(order, view)
        super(top_margin: 15)
        @order = order
        @view = view
        repeat(:all) do
          text "Envelope ID: CA7C190B-6ED5-44C5-95F0-36D49EC3CA38"
        end

        text "#{@order.document_template.content.to_plain_text}", size: 12


        string = "page <page> of <total>"
        options = {
          :at => [bounds.right - 150, 0], :width => 150,
          :align => :right,
          :page_filter => (1..7), :start_count_at => 1,
          :color => "007700" }
        number_pages string, options
      end
    end
  end
end

image

image

@goulvench
Copy link

Worked around this issue using a bounding_box, as advised on Prawn's Google Group.

Here is how this would apply to your code:

def initialize(order, view)
  super(top_margin: 15)
  @order = order
  @view = view
  repeat(:all) do
    text "Envelope ID: CA7C190B-6ED5-44C5-95F0-36D49EC3CA38"
    @repeat_height = y # Store repeated content height
  end

  # Prevent text from overlapping repeated content
  bounding_box([bounds.left, bounds.top - @repeat_height], width: bounds.width, height: bounds.height - @repeat_height) do
    text "#{@order.document_template.content.to_plain_text}", size: 12
  end

  string = "page <page> of <total>"
  options = {
    :at => [bounds.right - 150, 0], :width => 150,
    :align => :right,
    :page_filter => (1..7), :start_count_at => 1,
    :color => "007700" }
  number_pages string, options
end

@rmosolgo
Copy link

Thanks for the clue on this! I was working on a similar problem, and although I had to shift around some of the calculations, this approach worked great for me. I ended up with roughly:

    prawn_doc.repeat(:all) do
      # Draw the header 
      repeat_height = prawn_doc.cursor
    end
    
    prawn_doc.bounding_box(
      [prawn_doc.bounds.left, repeat_height],
      width: prawn_doc.bounds.width, 
      height: repeat_height
    ) do
      # draw _all_ the rest of the document content, regardless of pages 
    end   

I'm still getting started with Prawn, so I'm not quite sure why had to use .cursor and remove some of the subtractions, but ... it seems to work!

@amitpatelx
Copy link

Thanks @goulvench for sharing solution.

I am using include Prawn::View over extending < Prawn::Document and so getting following error on calling y method for @repeat_box_height = y

NoMethodError: undefined method `end_line=' for nil:NilClass
from /usr/local/lib/ruby/3.2.0/psych/tree_builder.rb:133:in `set_end_location'

Here is the solution that worked for me

repeat(:all) do
  ...
  ....
  @repeat_box_height = cursor # Store repeated content height
end

bounding_box([bounds.left, @repeat_box_height], width: bounds.width, height: @repeat_box_height) do
  ...
  ....
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants