Search code examples
rubyreportprawn

Align multiple text ruby prawn report


Hi I'm creating a PDF with prawn, how can I do to align the texts here that you see in the image below at the same height?

PDF Image:

enter image description here

Ruby Code:

Prawn::Document.generate("my.pdf") do
  image logo,:width=>540,:height=>60
  text "Spett.le ",:align=>:right
  move_down 5
  text "\n"+ragionesociale, :align => :right
  text "Data:  "+stringDate
  move_down 10
  text "Nome Cantiere:     "+nomecantiere
  move_down 30
  text "Note: "+note
  move_down 30
  end

Solution

  • Aligning strings to the left and right in the same line

    To have one string aligned to the left and another aligned to the right in the same line you could use float.
    float resets the cursor after printing a given text, meaning if you didn't use align: :right for the next text, it would be on the same spot as the first text, but if you do use the align: :right it results in what I believe you are asking for.
    If you want "Spett.le" and the date in the same line for example:

    float { text "Data: #{stringDate}", align: :left }
    text 'Spett.le', align: :right
    

    Of course you can also specify coordinates for everything, like suggested in some comments, and build the layout you want that way, but I find this solution here easier to use and maintain.