MPDF如何防止TD上的线路断裂
MPDF如何防止TD上的新线,我希望文本超过TD宽度,并且自动字符被“ ...”代替。 例如: png
我试图计算每个字符占据的宽度,并使用'...''...拼接多余部分,但仍然无法满足要求,有些字符仍然会读取销售线 png png
php code
$config = [
'mode' => 'utf-8',
'margin_top' => 13,
'margin_left' => 10,
'margin_right' => 10,
'format' => 'A4',
'dpi' => 300,
];
$mpdf = new Mpdf($config);
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
$html = file_get_contents('./test.html');
$mpdf->SetFont('sun-exta', null, 8.16);
$subStr2 = function (string $s) use ($mpdf) {
$totalWidth = 167.11608661417;
$w = 0;
$dotWidth = 3 * $mpdf->GetCharWidthNonCore('.', false) * Mpdf::SCALE;
$w += $dotWidth;
for ($i = 0, $clen = mb_strlen($s); $i < $clen; $i ++) {
$c = mb_substr($s, $i, 1, 'UTF-8');
$cw = ($mpdf->GetCharWidthNonCore($c, false) * Mpdf::SCALE);
$w += $cw;
if ($w > $totalWidth) {
return mb_substr($s, 0, $i, 'UTF-8') . '...';
}
}
return $s;
};
$pros = [
[1,"算法算法算法算法算法", "型号", "个", "10", "11.00", "111.00", "-"],
[2,"翻法春天带着一阵春雨悄悄得走来了春雨淅淅沥沥地下", "型号", "个", "10", "11.00", "111.00", "-"],
[3,"vasadHs算asdasHhsdf算法算OEHKSHs法算法算法", "型号", "个", "10", "11.00", "111.00", "-"],
[4,"shdf eritue kl翻 (-)sdhfeyytk s kdhfshfk word word word word word word word", "型号", "个", "10", "11.00", "111.00", "-"],
];
foreach ($pros as $item) {
$table .= '<tr>'
. '<td style="width: 132px;">' . $item[0] . '</td>'
. '<td style="width: 715px;">' . $subStr2($item[1]) . '</td>'
. '<td style="width: 269px;">' . $item[2] . '</td>'
. '<td style="width: 194px;">' . $item[3] . '</td>'
. '<td style="width: 194px;">' . $item[4] . '</td>'
. '<td style="width: 213px;">' . $item[5] . '</td>'
. '<td style="width: 286px;">' . $item[6] . '</td>'
. '<td style="width: 245px;">' . $item[7] . '</td></tr>';
}
$mpdf->writeHtml(str_replace('{{Tdata}}', $table, $html));
$mpdf->Output( './test.pdf', Destination::FILE);
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.table {
margin: 30px 0;
width: 100%;
border-right:4px solid #000;
border-bottom:4px solid #000;
border-collapse: collapse;
hyphens: none;
}
.table td, .table th {
hyphens: none;
text-align: center;
border-left:4px solid #000;
border-top:4px solid #000;
empty-cells:show;
}
.table th{
height: 68px;
}
.table td {
height: 110px;
font-size: 34px;
}
</style>
</head>
<body>
<table class="table">
<thead>
<tr>
<th style="width: 132px;">num</th>
<th>product name</th>
<th style="width: 269px;">unit</th>
<th style="width: 194px;">type1</th>
<th style="width: 194px;">type</th>
<th style="width: 213px;">number</th>
<th style="width: 286px;">price</th>
<th style="width: 245px;">remark</th>
</tr>
</thead>
<tbody>
{{Tdata}}
</tbody>
</table>
</body>
</html>
输出file test.pdf test.pdf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论