You have to provide the width of the text box, otherwise the text_box
will use the page width minus the start position.
In order to calculate the string width, you should use the width_of
method provided by the Prawn library, but, for some reason, Prawn doesn't recognise the \n
char in width_of
method, then you must have to split it by hand and calculate the line widths separately, which is not so difficult:
def text_width(pdf, text)
# slip by new line char and find maximum text width
text.split("\n").map {|s| pdf.width_of(s)}.max
end
Inserting text_width
in your code:
pdf.text_box s = "Here's some left text,\nbla bla bla",
:at => [20, page_height],
:align => :left,
:width => text_width(pdf, s)
pdf.text_box s = "Here's some center text,\nbla bla bla",
:at => [20, page_height-50],
:align => :center,
:width => text_width(pdf, s)
pdf.text_box s = "Here's some right text,\nbla bla bla",
:at => [20, page_height-100],
:align => :right,
:width => text_width(pdf, s)