prawnpdf / prawn-table

Provides support for tables in Prawn
Other
205 stars 97 forks source link

Word wrap in cell #67

Open alpracka opened 8 years ago

alpracka commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 1 year ago

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)