Tintin81
Tintin81

Reputation: 10207

Text indented on the right in PDFs generated with Prawn

I am using Prawn to render PDFs in my Rails app.

For some reason, though, my phone numbers are always indented on the right by 2 (?) spaces.

Can anybody tell me what I am missing here? All three values are saved as strings in my SQLite database and there are no whitespace characters that I am aware of.

The e-mail and url values are always neatly aligned to the right.

  def show_sender_details
    text "#{p.telephone}\n#{p.email}\n#{p.url}", :align => :right 
  end

Can anybody help?

Upvotes: 4

Views: 2058

Answers (4)

Tintin81
Tintin81

Reputation: 10207

OK, 4 years later I can finally answer my own question (yay!): It turns out that I had some invisible \r characters in my database. Not sure how they got in there... (By the way: I have moved my app from SQLite to MySQL in the meantime.)

All I needed to do was remove those characters:

telephone.tr("\r", "")

Upvotes: 0

Yolgie
Yolgie

Reputation: 289

Ok, if you problem is related to the hyphens as well, the solution is to use a Prawn Release >= 0.13.1 (not any of the old 1.0.0.rc)

See GitHub: Prawn Issue 578

Upvotes: 0

drosboro
drosboro

Reputation: 398

It really does seem most likely that there's some stray whitespace in the p.telephone field. I'm doing something similar to you with Prawn / Rails, so I just tried as many ways as I could think of to get it not to right-align correctly, and inserting whitespace into the field was the only way I could reproduce your problem.

Even if it's not your specific issue in this case, it's probably a good idea to strip whitespace off of your telephone numbers (and in fact, the other fields as well) before you render them anyways - presuming this is a form-field somewhere, you can expect that some user is going to put in trailing spaces by accident. You can either do that in your PDF code, like this:

text "#{p.telephone.strip}\n#{p.email.strip}\n#{p.url.strip}", :align => :right

or you could do it in your model if you'd like:

def telephone=(t)
  write_attribute(:telephone, t.strip)
end

I personally like the first option (do it in the PDF view code), because I'd prefer to store exactly what the user enters, and only manipulate their data when necessary (at the view), but it does tend to clutter up the view code a bit.

Upvotes: 1

Yevgeniy Anfilofyev
Yevgeniy Anfilofyev

Reputation: 4847

Phone numbers are always indented on the right because in the text method is used :align => :right, so all strings of text aligned to the right.

You could try to use text_box method with left alignment and put this box manualy on the page by defining indentation of its top-left corner from the top and right edges of the page (bounds.right and bounds.top values). Something like this (full working example):

require 'prawn'
require 'ostruct'

p = OpenStruct.new(
  :telephone => "+1-201-555-2233",
  :email => "[email protected]",
  :url => "http://example.com")

Prawn::Document.generate('simple_text.pdf', :skip_page_creation => true) do
  start_new_page
  text_box("#{p.telephone}\n#{p.email}\n#{p.url}",
    :at => [bounds.right - 100, bounds.top - 100],
    :align => :left,
    :height => 50,
    :width => margin_box.width)
end

Upvotes: 2

Related Questions