从HTML生成PDF的Rails命令

4
我在Rails中使用HTML生成PDF遇到了一些问题。我尝试使用PDFKitWicked_PDF宝石,但两者都存在页面断行问题,它们会在<tr>内部断行
让我展示一下我使用Wicked_PDF时正在做什么:
在我的控制器中:
render pdf: "report", 
       :template => "reports/index.html.erb",
       :layout => "pdf",
       :orientation => 'Landscape',
       footer: {
         right: "Page [page] of [topage]",
         font_size: 9
       }

layouts/pdf.html.erb:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">

  <style>
      .table {
          border-bottom: 1px solid #ddd;
          border-collapse: collapse;
          border-spacing: 0;
          margin-bottom: 20px;
          width: 100%;
      }

      td,
      th {
          border-top: 1px solid #ddd;
          padding: 2px;
          text-align: left;
          vertical-align: middle;
      }

      .table > thead th {
          background: #f7f7f7;
      }
  </style>
</head>

<body>

<%= yield %>

</body>
</html>

reports/index.html.erb:

<table class="table">
  <thead>
  <tr>Example of a text that breaks the tr</tr>
  </thead>
  <tbody>
  <% 3.times do %>
      <tr>
        <td><%= "string " * 500 %></td>
      </tr>
  <% end %>
  </tbody>
</table>

也尝试使用了这个 CSS 属性:

tr, td, th, tbody, thead, tfoot {
  page-break-inside: avoid !important;
}

注意:我认为展示我使用的PDFKit所做的内容并不相关,因为HTML和CSS没有改变,只是呈现方式有所不同,问题完全相同。
我错过了什么?
欢迎使用其他宝石解决方案。
1个回答

0
对于PDFkit,您还可以使用此属性。

.table{
  tr{
    page-break-before: always !important;
  }
}

你也可以尝试一些在这里提到的解决方法。


@RuiSchnaiderJúnior 你使用的是哪个版本的wkhtmltopdf库?页面分页在旧版本中是已知问题。 - Shifa Khan
版本是wkhtmltopdf-0.12.2.1-1.fc20.x86_64。 - Rui Schnaider Júnior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接