Question

I have footer text that may be more than one line, the length of the text will vary depending on the user's name and the company they work for. Like all footers, it needs to be displayed below the document's bottom bound so that it doesn't get intermingled with the main content of the PDF.

The problem is that the only why I have found in Prawn to get text printed below the document's bottom bound is by using #draw_text. This is the same method that number_pages uses to get its text to appear below the document's bottom bound. However the one caveat of using #draw_text appears to be its inability to wrap text to a second line.

I have found many methods that allow me to wrap text to a second line such as #text_box, #bounding_box, etc. but the caveat of these methods is that they don't allow you to print anything below the document's bottom bound.

For example, the following will not print anything on the document because it would be below the document's bottom bound:

text_box "Generated by Tom Cruise for Universal Studios", :at => [bounds.left, 0], :width => 200

The following does print on the document because it is within the document's bottom bound but will also be printed on top of any content that already exists there:

text_box "Generated by Tom Cruise for Universal Studios", :at => [bounds.left, bounds.bottom - 20], :width => 200

And finally the following will print below the document's bottom bound ensuring that it is not being printed on top of any existing content in the PDF, but there is no available :width option or the ability to have the text wrap to a second line if needed:

draw_text "Generated by Tom Cruise for Universal Studios", :at => [bounds.left, 0]

Is there a way to get the best of both worlds? A way to print below the document's bottom bound AND enforce a maximum width with line wrapping?

Was it helpful?

Solution 2

I ended up writing my own little routine to handle multiple lines in the footer. It'd be nice if Prawn supported something like this out of the box, I'm still a bit mystified why some things can't be displayed below the bottom bound while other things can be. It would also be nice if all the different types of text methods supported the :width attribute with line wrapping...but I digress, here is the code I ended up using:

line_wrapper = Prawn::Core::Text::LineWrap.new

repeat :all do
  str = "Generated on " + Time.zone.now.strftime("%m/%d/%y at %I:%M:%S %p %Z") + " by #{user.full_name} at #{user.company.name}"
  starting_position = 0
  while !str.blank?
    single_line = line_wrapper.wrap_line(str, :width => 470, :document => pdf)
    draw_text(single_line, :at => [bounds.left, starting_position])
    starting_position -= 10
    str.slice!(single_line)
  end
end

OTHER TIPS

I suspect you'll need to do the line wrapping manually (e.g. calculate when to break).

But I was able to get a multi-line footer using the standard number_pages method and the following:

pdf.number_pages "Copyright #{Time.now.year} Company.", [pdf.bounds.left, 0]
pdf.number_pages "Profile generated on #{Time.now.strftime('%B %d, %Y')}.", [pdf.bounds.left, 10]

Is that what you are looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top