【问题标题】:Set Table Column Width Less Than Content设置表格列宽小于内容
【发布时间】:2016-09-20 12:10:15
【问题描述】:

我在我的 Java 代码中使用 Flying Saucer PDF 生成库将 HTML 模板转换为 PDF。该模板由一个具有固定列数的表组成。我希望每列都有固定的宽度,并且其中的内容在溢出时隐藏。我为该任务编写了以下 CSS -

.col1 {
    width: 50px;
    max-width: 50px;
    min-width: 50px;
}
.col2 {
    width: 70px;
    max-width: 70px;
    min-width: 70px;
}
.col3 {
    width: 120px;
    max-width: 120px;
    min-width: 120px;
}
td, th {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

如果内容超出列宽,显然这不会限制列宽。该模板在 Chrome 和 Firefox 中看起来不错,但在生成的 PDF 中却没有。

有没有办法使用 Flying Saucer PDF 限制每列的宽度?

【问题讨论】:

    标签: html css pdf flying-saucer


    【解决方案1】:

    您可以在表格中添加以下样式:table-layout:fixed

    例子:

    <html>
        <head>
            <style type="text/css">
                .col1 {width: 50px;max-width:50px;min-width: 50px;}
                .col2 {width: 70px;max-width: 70px;min-width: 70px;}
                .col3 {width: 120px;max-width: 120px;min-width: 120px;}
                table{table-layout:fixed;}  
                td, th {
                    border:1px solid red
                    overflow: hidden;
                    white-space: nowrap;
                }
        </style>
        </head>
        <body>
          <table >
            <thead>
              <tr>
                <td class="col1">Col1</td>
                <td class="col2">Col2</td>
                <td class="col3">Col3</td>
              </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="col1">Lorem ipsum dolor sit amet, consectetur </td>
                    <td class="col2">la, molestie vel diam vitae, bibendum consequat enim. </td>
                    <td class="col3">s non metus. Donec auctor ipsum in quam bibendum, sit a</td>
                </tr>
            </tbody>
          </table>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2011-02-01
      • 2010-10-30
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      相关资源
      最近更新 更多