spipu / html2pdf

OFFICIAL PROJECT | HTML to PDF converter written in PHP
http://html2pdf.fr/en/default
Open Software License 3.0
1.68k stars 749 forks source link

Allow to break line on table cells #84

Closed LeonanCarvalho closed 6 years ago

LeonanCarvalho commented 8 years ago

I would like to propose a enhancement, if you allow me :bowtie:

There is a properties named word-wrap that works with word-break to break word preventing it to overflow the parent width, this property specifies line break opportunities within words.

For word-break Values have the following meanings:

<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "<br />\n", true);
/**
* Based on @cmbuckley  original function I implemented a function to wrap my texts with line break,   
* in all cases this is the best function, fit the word base on width on the table cell. 
* When the word is  too long it will be break in a 2 lines also. 
* Similar of this style: `style="word-wrap: break-word;  word-break: break-all;"` 
**/

<?php
function smart_wordwrap($string, $width = 75, $break = "<br>") {
// split on problem words over the line length
        $pattern = sprintf('/([^ ]{%d,})/', $width);
        $output = '';
        $words = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

        foreach ($words as $word) {
            // normal behaviour, rebuild the string
            if (false !== strpos($word, ' ')) {
                $output .= $word;
            } else {
                // work out how many characters would be on the current line
                $wrapped = explode($break, wordwrap($output, $width, $break));
                $count = $width - (strlen(end($wrapped)) % $width);

                // fill the current line and add a break
                $output .= substr($word, 0, $count) . $break;

                // wrap any remaining characters from the problem word
                $output .= wordwrap(substr($word, $count), $width, $break, true);
            }
        }

        // wrap the final output
        return wordwrap($output, $width, $break);
    }

The result into a table cell of this last is it: capturar

But as I'm applying a fixed width it on original string not on text value of parsed on html2pdf class. It would be very useful if it respect the cell width(if defined) to break the words into lines to prevent table overflow.

spipu commented 6 years ago

automatic break line is already there in HTML2PDF, it uses "usual rules", it doest not break words.

Breaking words is too dongerous, and it is not the solution for me : if your content does not fiit on your container => then the pb is maybe the container ...

you are creating a PDF for important document, cutting word automatically is not the solution.

sorry, but i will not implement this.

MockingMagician commented 5 years ago

Hello, and sorry for a new opening on it. But break words is an implemented CSS feature. So why not alloawing it here with the "word-break" css feature ?

k-zakhariy commented 3 years ago

Hello, and sorry for a new opening on it. But break words is an implemented CSS feature. So why not alloawing it here with the "word-break" css feature ?

yeah, for ex i have long filename some_long_filename_20210702_somehashcodehere_anotherlongtext.json