关于php:Html2pdf不支持断字:break-all CSS

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

hai,每个人我都使用html2pdf,它不支持断字:break-all css有什么想法吗?

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

输出pdf的宽度超过字符串长度大小的30%

输出pdf:testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets

我想要输出:

testtestetstetstetstetstettstets
tetstetstetstetstetstetstetstetstets


好吧,那很复杂。您的测试字符串太长,但是它不是由多个单词组成。这意味着断字是行不通的,因为没有任何要断的字。显然,这可能只是一个示例,在这种情况下,html2pdf可能不支持相对宽度和word-break,因此您可以尝试使用绝对宽度和word-break

就是说,这是我知道可以使用的东西:PHP中的自动换行。因此,可以使用echo wordwrap($yourvar, 75,"\
", true)
代替echo $yourvar;,这将始终剪切字符串,即使它只是一个长字符串也是如此。花费一些时间来使字符数与您要查找的宽度相匹配,但这是可行的。

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

输出:

1
2
3
testtesttesttesttest
testtesttesttesttest
testtest


试试这个;

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

不是word-break而是word-wrap;


如果您希望长字符串一致地包裹在边界容器中,我认为您应该能够通过在原始字符串的每个字母之间插入零宽度空格字符( \\xe2\\x80\\x8b)来实现这一点。这将具有包装效果,就好像每个字符都是其自己的单词一样,但是不会向最终用户显示空格。这可能会导致您在最终产品上进行文本搜索或建立索引时遇到麻烦,但是从美学角度来看,它应该可以可靠地完成任务。

从而:

1
testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets

成为

1
t e s t t e s t e t s t e t s t e t s t e t s t e t t s t e t s t e t s t e t s t e t s t e t s t e t s t e t s t e t s t e t s t e t s

(显示为:" tst te s t e ts s t t s t e s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s t s s t s t s s t s s t s s t s s t s t s s)

因此,如果将其包装,它将完全包装到其容器的边界。这是一个小例子。

只需编写一个PHP脚本来遍历字符串并插入空格:

1
2
3
4
5
6
7
8
9
$string="testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets";
$new_string ="";
for($i=0;$i<strlen($string);$i++){
   if ($string[$i]==' ' || $string[$i+1]==' '){ //if it is a space or the next letter is a space, there's no reason to add a break character
      continue;
   }
   $new_string .= $string[$i]."";
}
echo $new_string

这是一个特别好的解决方案,因为与wordwrap()不同,它会自动调整非固定宽度的字体(基本上是实际使用的字体的99%)。

同样,如果您需要使生成的PDF可搜索,这不是一个好方法,但是它将使它看起来像您想要的那样。


您只需在代码中使用substr函数。
我为此举了一个例子。首先将您的输出变量。

1
2
3
$get_value ="testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);

等等。


在您的测试中,单词break无效,因为单词break仅在特定句子中的单词之间起作用。因此,您可以使用多个单词的句子,然后尝试使用分词系统


您可以使用"\
\
"
打印换行符。确保使用双引号。如果您的字符串在变量中,则需要使用字数统计功能并附加此字符串。您也可以使用PHP_EOL避免平台依赖性。


html2pdf不支持断字:break-all CSS

参考:http://www.yaronet.com/zh-CN/posts.php?sl=&h=0&s=151321#0


我想对HTML2PDF和表格添加一些自己的经验。

我使用此解决方案生成了PDF,其中包含一个表格,该表格中填充有交货确认书(产品列表)。这样的列表可能包含多达数千个产品(行)。

我在单元格中遇到了格式化和长字符串的问题。第一个问题是即使我将表的宽度设置为100%且表头(

)列的宽度也变得太宽(HTML2PDF不支持

,所以我无法在全局范围内定义它)-有些列超出可见区域。我使用wordwrap()
作为分隔符来分解看起来像在工作的长字符串。不幸的是,事实证明,如果第一行和最后一行中有这么长的字符串,则整个表都将被前缀并附加空白页。不是一个真正的bugger,但看起来也不好。最终解决方案是(适用于宽度可能超过可见区域的表):

  • 设置表格和每行的固定宽度(以像素为单位)

    • 对于A4字母大小,我使用的总宽度为550像素,并带有默认边距,但您必须花一点时间才能在列之间分配宽度
  • wordwrap中使用空格或 / \\xe2\\x80\\x8b作为分隔符

对于您要散布100%可见区域宽度的小桌子,可以使用%中表示的宽度。


您可以使用此方法。

1
2
3
4
5
6
<?php
$get_value ="testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);
$third = substr("$get_value",8,11);
?>

我认为此功能是一种脚的解决方案。

1
2
3
4
5
6
7
8
9
10
11
12
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;
    }