Three pieces of text, separately aligned, on the same line...

1,294 views
Skip to first unread message

Mel Riffe

unread,
Feb 7, 2012, 9:43:18 PM2/7/12
to prawn...@googlegroups.com
Howdy,

I've recently started using Prawn, not in anger, but definitely in earnest; I'm converting PDF::Writer documents into Prawn::Documents. I implemented one of our reports as a Spike and was able to accomplish ~99% of the existing document. The piece I wasn't able to directly replicate is a line I have that has one piece of text left-aligned, another piece of text center-aligned, and a third piece of text (which contains my page numbers) right-aligned, all on the same line.

This is my current implementation:

pdf.repeat :all, :dynamic => true do
  pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom + 45], :width => pdf.bounds.right, :height => 45) do
    pdf.stroke do
      pdf.horizontal_rule
    end
    pdf.text "I'm on the left side", :align => :left
    pdf.text "I'm in the middle", :align => :center
    pdf.text "below the middle text", :align => :center
    pdf.text "below below the middle text", :align => :center
    string = "Page <page> of <total>"
    options = {
      :at => [pdf.bounds.right - 150, pdf.bounds.bottom + 45],
      :width => 150,
      :align => :right
    }
    pdf.number_pages string, options
  end
end

It /mostly/ works but "I'm in the middle" isn't on the same line as "I'm on the left side" and "Page <page> of <total>".

Am I missing something? How can I accomplish my goal of having three separately aligned pieces of text, one being for page numbers, all on the same line?

Cheers,
Mel

Brad Ediger

unread,
Feb 7, 2012, 9:57:37 PM2/7/12
to prawn...@googlegroups.com
On Tue, Feb 7, 2012 at 8:43 PM, Mel Riffe <mri...@gmail.com> wrote:
> Howdy,
>
> I've recently started using Prawn, not in anger, but definitely in earnest;
> I'm converting PDF::Writer documents into Prawn::Documents. I implemented
> one of our reports as a Spike and was able to accomplish ~99% of the
> existing document. The piece I wasn't able to directly replicate is a line I
> have that has one piece of text left-aligned, another piece of text
> center-aligned, and a third piece of text (which contains my page numbers)
> right-aligned, all on the same line.

Prawn::Document#float draws its content and then resets the
y-position. Here's the source:

https://github.com/sandal/prawn/blob/master/lib/prawn/document.rb#L330

-be

Mel Riffe

unread,
Feb 8, 2012, 8:50:12 AM2/8/12
to prawn...@googlegroups.com
Hi Brad,

Thanks for the feedback. I'll give it a go.

Cheers,
Mel
Reply all
Reply to author
Forward
0 new messages