简体   繁体   English

DomPDF 表格固定列宽并打破长文本

[英]DomPDF table fixed column width and break long text

I have a table with very long texts that has no spaces (users like to post a full website URL complete with all the parameters)我有一个没有空格的表格很长的文本(用户喜欢发布一个完整的网站 URL 完成所有参数)

Then I set on word-break:break-all;然后我设置了word-break:break-all; and word-wrap:break-word;word-wrap:break-word; on each <td> hoping that the text could fit on the table.在每个<td>希望文本可以放在桌子上。

I also set all the <td> / column widths to some specified amount.我还将所有<td> / 列宽设置为某个指定的量。

I use Laravel's DomPDF wrapper here: https://github.com/barryvdh/laravel-dompdf我在这里使用 Laravel 的 DomPDF 包装器: https://github.com/barryvdh/laravel-dompdf


Case 1: No table-layout: fixed案例1:没有table-layout: fixed

<table class="bordered">
    <tr class="font-12">
        <th style="width: 25px">No</th>
        <th style="width: 100px">Nama Alat</th>
        <th style="width: 25px">Jml</th>
        <th style="width: 300px">Spesifikasi</th>
        <th style="width: 300px">Supplier</th>
        <th style="width: 150px">Gambar</th>
    </tr>
    @foreach ($items as $index => $item)
    <tr>
        <td style="width: 25px">{{ ($index+1) }}</td>
        <td style="width: 100px">{{ $item['name'] }}</td>
        <td style="width: 25px">{{ $item['jumlah_disetujui'] }}</td>
        <td class="font-12" style="width: 300px; word-break:break-all; word-wrap:break-word;">
            {!! nl2br( $item['spesifikasi'] ) !!}
        </td>
        <td class="font-12" style="width: 300px; word-break:break-all; word-wrap:break-word;">
            {!! nl2br( $item['supplier'] ) !!}
        </td>
        <td style="width: 150px">
            @if ($item['image'])
                <img style="max-height: 125px;" src="{{ $item['image'] }}" />
            @else
                -
            @endif
        </td>
    </tr>
    @endforeach
</table>

Case 1 PDF Result: Word-wrap does not work, column width works on cells with few texts, but does not work either on cells that has long texts (needs word-wrap).案例 1 PDF 结果:自动换行不起作用,列宽适用于文本很少的单元格,但也不适用于文本较长的单元格(需要自动换行)。

自动换行不起作用


Case 2: Adding table-layout: fixed案例2:添加table-layout: fixed

<table class="bordered" style="table-layout: fixed"> <!-- only added this -->
    <tr class="font-12">
        <th style="width: 25px">No</th>
        <th style="width: 100px">Nama Alat</th>
        <th style="width: 25px">Jml</th>
        <th style="width: 300px">Spesifikasi</th>
        <th style="width: 300px">Supplier</th>
        <th style="width: 150px">Gambar</th>
    </tr>
    @foreach ($items as $index => $item)
    <tr>
        <td style="width: 25px">{{ ($index+1) }}</td>
        <td style="width: 100px">{{ $item['name'] }}</td>
        <td style="width: 25px">{{ $item['jumlah_disetujui'] }}</td>
        <td class="font-12" style="width: 300px; word-break:break-all; word-wrap:break-word;">
            {!! nl2br( $item['spesifikasi'] ) !!}
        </td>
        <td class="font-12" style="width: 300px; word-break:break-all; word-wrap:break-word;">
            {!! nl2br( $item['supplier'] ) !!}
        </td>
        <td style="width: 150px">
            @if ($item['image'])
                <img style="max-height: 125px;" src="{{ $item['image'] }}" />
            @else
                -
            @endif
        </td>
    </tr>
    @endforeach
</table>

Case 2 PDF Result: Word-wrap works, but column width has no effect案例 2 PDF 结果:自动换行有效,但列宽无效

自动换行有效,但列宽无效


What I want is: a table with fixed width columns that has a working word-wrap我想要的是:一个具有固定宽度列的表格,该表格具有有效的自动换行

Is this impossible to do on DomPDF?这在 DomPDF 上不可能吗?
Any workaround or 'hacks' that can help achieve this?任何可以帮助实现这一目标的解决方法或“黑客”?

Or maybe I should start looking for other / better PDF generators?或者也许我应该开始寻找其他/更好的 PDF 发电机?

EDIT:编辑:
I also found a lot of Github Issues on this, with no sign of the bug being fixed我还发现了很多 Github 问题,没有迹象表明该错误已修复

我用table-layout设置宽度%:固定,它工作正常

Not really an "answer", but my solution is switching to Snappy PDF (used the Laravel Package ). 这不是一个“答案”,但我的解决方案是切换到Snappy PDF (使用Laravel包 )。
Worked like a charm, I did not even change the HTML markups. 工作就像一个魅力,我甚至没有改变HTML标记。

I had the same problem, I solved it thanks to @Solonka's answer. 我遇到了同样的问题,由于@ Solonka的回答,我解决了这个问题。

Example: 例:

 <table border="1px" style="table-layout:fixed;"> <tr> <th style="width:5%;">5%</th> <th style="width:10%;">10%</th> <th style="width:25%;">25%</th> <th style="width:60%;">60%</th> </tr> </table> 

Width calculation in DomPDF is problem (td - in mm), but the functional solution is use "border-box": DomPDF 中的宽度计算是问题(td - 以毫米为单位),但功能解决方案是使用“边框框”:

*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;

For me this worked:对我来说,这很有效:

table{
      max-width: 750px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM