Indented text on the right in PDF files created with shrimp

I use Prawn to render PDF files in my Rails application.

For some reason, my phone numbers always recede on the right into 2 (?) Spaces.

Can someone tell me what I am missing here? All three values ​​are stored as strings in my SQLite database, and I don't know whitespace.

Email 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 

Does anyone help?

+4
source share
4 answers

Phone numbers always recede to the right, because the text method uses :align => :right , so all lines of text are aligned to the right.

You can try using the text_box -aligned text_box method and place this block on the page manually by specifying the top and right edges of the page in the upper left corner (bounds.right and bounds.top)). Something like this (full working example):

  require 'prawn'
 require 'ostruct'

 p = OpenStruct.new (
   : telephone => "+ 1-201-555-2233",
   : email => " test@example.com ",
   : 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 
+2
source

It is likely that there are spaces in the p.telephone field. I am doing something similar to you using Prawn / Rails, so I tried as many ways as I could think to make it not align correctly, and inserting a space into the field is the only way to reproduce your problem.

Even if this is not your specific problem in this case, it would probably be nice to exclude spaces from your phone numbers (and, in fact, other fields) before you render them anyway - suppose it's a form somewhere in elsewhere, you can expect some user to accidentally fit in trailing spaces. You can do this in your PDF code, for example:

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

or you can do it in your model if you want:

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

I personally like the first option (do it in the PDF viewing code), because I prefer to store exactly what the user enters, and only manipulate their data when necessary (by sight), but it tends to clutter up the presentation code a bit.

+1
source

Well, if the problem is with hyphens, the solution is to use the release Prawn> = 0.13.1 (not any old 1.0.0.rc)

See GitHub: 578 Shrimp Problem

0
source

OK, 4 years later I can finally answer my question (yay!): It turns out I had some invisible \r characters in my database. I don’t know how they got there ... (By the way: I moved my application from SQLite to MySQL in the meantime.)

All I had to do was remove these characters:

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

Source: https://habr.com/ru/post/1484257/


All Articles