Html2pdf doesn't support word-break:break-all

2020-07-10 06:42发布

hai everybody i am using html2pdf ,it doesn't support word-break:break-all css any idea?

example

   <td style="width:30%;word-break:break-all ;">
      testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
    </td>

output pdf take above 30% width like string length size

output pdf: testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets

I want Output :

testtestetstetstetstetstettstets
tetstetstetstetstetstetstetstets

10条回答
疯言疯语
2楼-- · 2020-07-10 07:04

Well, that's complicated. Your teststring is too long, but it's not composed of multiple words. That means that word-break won't work, because there aren't any words to break on. Obviously, this might well just be an example, in which case it might be that html2pdf just doesn't support relative widths and word-break, so you could try having an absolute width and word-break.

That said, here's something I know that will work: wordwrap in PHP. So, instead of echo $yourvar; you could use echo wordwrap($yourvar, 75, "\n", true) instead, which will always cut the string, even if it's just one long string. It takes a little fiddling to get the number of characters to match up with the width that you're looking for, but it will work.

<?php
$foo = str_repeat('test',12);
echo wordwrap($foo, 20, '<br />', true);

Output:

testtesttesttesttest
testtesttesttesttest
testtest
查看更多
太酷不给撩
3楼-- · 2020-07-10 07:06

I want to add little bit of own experience with HTML2PDF and tables.

I used this solution to generate the PDF containing a table filled with delivery confirmation (list of products). Such list may contain up to thousand of products (rows).

I encountered a problem with formatting and long strings in cells. First problem was that the table was getting too wide even if I set the table's width to 100% and the width of header (<th>) columns (HTML2PDF does not support <colgroup> so I couldn't define it globally) - some columns were out of visible area. I used wordwrap() with <br /> as separator to break down the long strings which looked like it's working. Unfortunately, it turned out that if there is such long string in first and last row the whole table is prepended and appended with empty page. Not a real bugger but doesn't look nice either. The final solution was to (applies for tables which width could outreach the visible area):

  • set the fixed widths of table and each row in pixels
    • for A4 letter size I am using total width of 550 px with default margins but you'd have to play around a little to distribute the width between columns
  • in wordwrap use empty space or &#8203; / \xe2\x80\x8b as delimiter

For small tables that you'd like to spread for 100% of visible area width it is OK to use width expressed in %.

查看更多
放荡不羁爱自由
4楼-- · 2020-07-10 07:07

You may use this method.

<?php
$get_value = "testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);
$third = substr("$get_value",8,11);
?>
查看更多
forever°为你锁心
5楼-- · 2020-07-10 07:08

try this;

<td style="width:30%; word-wrap:break-word;">
   testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
</td>

not word-break it is word-wrap ;

查看更多
▲ chillily
6楼-- · 2020-07-10 07:10

I think this function is a limping solution.

function String2PDFString($string,$Length)
    {
    $Arry=explode(" ",$string);
    foreach($Arry as $Line)
        {
        if(strlen($Line)>$Length)
            $NewString.=wordwrap ($Line,$Length," ",true);
        else
            $NewString.=" ".$Line;
        }
    return $NewString;
    }
查看更多
叼着烟拽天下
7楼-- · 2020-07-10 07:12

html2pdf does not support this word-break:break-all css

Ref: http://www.yaronet.com/en/posts.php?sl=&h=0&s=151321#0

查看更多
登录 后发表回答