MPDF如何防止TD上的线路断裂

发布于 2025-01-27 16:39:17 字数 3859 浏览 2 评论 0 原文

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

How does mpdf prevent newlines on td, I want the text to exceed the td width and the automatic characters are replaced with '...'
eg:
before png

I try to calculate the width occupied by each character, and use '...' to splicing the excess part, but it still can't meet the requirements, and some characters still read line break
after 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>

output file test.pdftest.pdf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文