Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word wrap in cell #67

Open
alpracka opened this issue Mar 21, 2016 · 5 comments
Open

Word wrap in cell #67

alpracka opened this issue Mar 21, 2016 · 5 comments

Comments

@alpracka
Copy link

Hello,

I saw few issues existing about cell sizing and wrapping but quite old so sorry if I'm creating any duplicate, but I can't figure how to disable word wrap by character in cell and I'm not sure whether it is bug or my lack of knowledge as I'm trying prawn-table first day.

pdf = Prawn::Document.new

rows = []
cells = []

cells << "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages"
cells << "Word"
cells << "Longword"

rows << cells

pdf.table(rows)

Output: https://timi.cz/system/example.pdf - I would expect words in the last two cells not to be wrapped by character.

Using the latest from master branch:

GIT
  remote: git://github.com/prawnpdf/prawn-table.git
  revision: da0423372f06f79c2b1d45c1d6a45f6e0381b7c9
  specs:
    prawn-table (0.2.3)
      prawn (>= 1.3.0, < 3.0.0)

and

prawn (2.1.0)

Thanks for help.

@simon-kr
Copy link

The question is, what would you expect? The two latter not to be wrapped at all, or to be wrapped typographically correctly? As far as I know, Prawn::PDF tries to calculate the reasonable width for a column. This may not always work out as expected. The main problem is, that the first cell could use all the width, so there is only reserved a minimum width for the other cells.

The first possibility:
You have to set fixed width to (part) of your columns like this:

cells << "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages"
cells << { content: "Word", width: 50 }
cells << { content: "Longword", width: 80 }

The second possibility:
Funny enough (I can't explain why), when you set soft hyphens (HTML: shy), it does NOT wrap. This is more like a quirk ;-)

cells << "..."
cells << "Wo#{Prawn::Text::SHY}rd"
cells << "Long#{Prawn::Text::SHY}word"

@alpracka
Copy link
Author

Thank you for suggestions, I will try the second option. The first is problematic with variable text inputs. To your question, I would expect the same behaviour as browsers do with HTML tables (https://jsfiddle.net/mjhpbequ/), so wrap only by white spaces and never wrap a single word.

@simon-kr
Copy link

You could calculate the needed width for the columns by finding the longest word for that column. If the longest word fits, Prawn will probably not wrap single characters but only break with spaces (like in the first column).

pdf.width_of(longest_word, size: 12) # + small margin probably

@ericfreese
Copy link

PR #68 may help here by allowing you to set a min_width for the last two columns in your example.

Going forward, I could see a text cell option for :word_wrap being added that sets the min_width of the cell to the width of the longest word in the cell.

@akostadinov
Copy link

akostadinov commented Mar 9, 2023

My opinion of what should be done is to first sizing should be tried that avoids splits on non-empty spaces any words (like #98). If this fails, then existing heuristics would be fine.

Having ability to set manually min_width would also be nice in certain situations, although often Prawn::Text::NBSP can help.

Just FYI presently one can end up with a field that is overly long while wrapping words in another.
image

This can be replicated with data like:

  LONG_ADDRESS = [%w[Name Farnsworth],
                  ['Address', %{JOHN "GULLIBLE" DOE\nCENTER FOR FINANCIAL ASSISTANCE TO DEPOSED NIGERIAN ROYALTY\n421 E DRACHMAN
  TUCSON AZ 85705-7598}],
                  %w[Country Patagonia]].freeze
                  
@pdf.table(LONG_ADDRESS, width: 90.mm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants