I know there are a lot of issues with wkhtmltopdf and page breaks that date back years, but I haven't worked out a solution yet. I'm using the PDFKit gem to generate my html pages into pdfs, but I don't want the pages to break in the middle of a table row.
I'm using wkhtmltopdf-binary (0.9.9.3), which looks to be the most updated version
My CSS:
@media print { #scores table tr td, #scores table tr th { page-break-inside: avoid !important; } table, tr, td, th, tbody, thead, tfoot { page-break-inside: avoid !important; } }
My table:
<div class="score_table"> <table id="scores" class="print-friendly"> <tbody> <% @chapters.each do |chapter| %> <tr> <th colspan="3" ><%= chapter.name %></th> </tr> <% chapter.rules.each do |rule| %> <tr> <th colspan="2" >Rule: <%= rule.name %></th> <th></th> </tr> <!-- Triggers --> <% rule.triggers.each do |trigger| %> <tr> <td>T</td> <td><%= markdown(trigger.body) %></td> <td><%= markdown(trigger.explanation) %></td> </tr> <% if trigger.image? || trigger.image2? %> <tr> <td></td> <% if trigger.image? %> <td><%= image_tag trigger.image.url(:thumb) %></td> <% else %> <td></td> <% end %> <% if trigger.image2? %> <td><%= image_tag trigger.image2.url(:thumb) %></td> <% else %> <td></td> <% end %> </tr> <% end %> <% end %> <!-- Questions --> <% rule.questions.each do |question| %> <tr> <td>Q</td> <td><%= markdown(question.body) %></td> <td><%= markdown(question.answer) %></td> </tr> <% if question.image? || question.image2? %> <tr> <td></td> <% if question.image? %> <td><%= image_tag question.image.url(:thumb) %></td> <% else %> <td></td> <% end %> <% if question.image2? %> <td><%= image_tag question.image2.url(:thumb) %></td> <% else %> <td></td> <% end %> </tr> <% end %> <% end %> <!-- Hints --> <% rule.hints.each do |hint| %> <tr> <td>H</td> <td><%= markdown(hint.body) %></td> <td><%= markdown(hint.explanation) %></td> </tr> <% if hint.image? || hint.image2? %> <tr> <td></td> <% if hint.image? %> <td><%= image_tag hint.image.url(:thumb) %></td> <% else %> <td></td> <% end %> <% if hint.image2? %> <td><%= image_tag hint.image2.url(:thumb) %></td> <% else %> <td></td> <% end %> </tr> <% end %> <% end %> <% end %> <% end %> </tbody> </table> </div>
Is there a work around, or is there something that I'm doing wrong? This is the result:

I could post the PDFKit code as well, but it sounds like a wkhtmltopdf issue
***Update - My CSS @print isn't affecting the page when .pdf is added to the url. I have my stylesheet link with media: "all"
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
Here's my initializer pdfkit.rb:
ActionController::Base.asset_host = Proc.new { |source, request| if request.env["REQUEST_PATH"].include? ".pdf" "file://#{Rails.root.join('public')}" else "#{request.protocol}#{request.host_with_port}" end }
If I can fix the CSS, then I probably will solve the page break issue!