4

I'm facing some problems to generate PDF from html with Rails. I tried to use PDFKit and Wicked_PDF gems, with both I had a problem with page break, they break inside a <tr>.

Let me show you what I'm doing with Wicked_PDF:

In my controller:

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>

Also tried to use this css property:

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

Note: I think it would not be relevant to show what I did with PDFKit, since the html and css doesn't change, only the way it is rendered and the problem is exactly the same.

What I've missed?

Solutions with other gems are welcome too.

1 Answers1

0

For PDFkit you can also use this property

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

You can also try a bunch of workarounds mentioned here

Community
  • 1
  • 1
Shifa Khan
  • 769
  • 6
  • 12