Rails从html

时间:2015-05-07 20:46:37

标签: html ruby-on-rails pdf-generation wicked-pdf pdfkit

我在使用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
       }

布局/ 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>

报告/ 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 :(得分:0)

对于PDFkit,您也可以使用此属性

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

您还可以尝试一些提及here

的变通办法