Search code examples
pdfxsltxsl-foapache-fop

XSL: can not truncate text on column


I'm using XSL to get an XML styled. The xsl defines a table with two columns. What i need to do is to get the text truncated at the end of the column.

I have truncated the text to a fixed amount of characters using the function substring but as the different characters has different size it results very ugly

Here is an example of what i want:

Supose that the strings that must be contained in the table are:

"short text"

"some text here"

"this is a very long text"

"this is a very very long text"

|     COLUMN 1       | COLUMN 2 |
|--------------------|----------|
|some text           | column 2 |
|some text here      | column 2 |
|this is a very long | column 2 |
|this is a very very | column 2 |

Does exist a xsl function that trunkates the string based on the size it fills on the column instead of the amount of characters?

Thanks in advance.


Solution

  • You place a block-container with sufficient height (greater than one line, but less than two lines) in the table cell, setting the overflow as hidden:

        <fo:table-cell>
            <fo:block-container overflow="hidden" height="15pt"><fo:block>this is a very, very, very long text here</fo:block></fo:block-container>
        </fo:table-cell>
    

    Note, this would chop off at a word boundary. If you want to chop off at arbitrary place then you would insert zero-width breaking spaces between each letter in the words. The above in a table shows it chopped at the last "very" below:

    enter image description here