繁体   English   中英

Rails从html生成PDF

[英]Rails generate PDF from html

我在使用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做了什么并不PDFKit ,因为html和css没有改变,只有它的呈现方式和问题完全相同。

我错过了什么?

其他宝石的解决方案也是受欢迎的。

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

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

您还可以尝试这里提到的一些变通方法

暂无
暂无

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

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