带有 pdfkit on Rails 的 WKHTMLTOPDF 忽略表格分页符 [英] WKHTMLTOPDF with pdfkit on Rails ignoring table page breaks
问题描述
我知道可以追溯到几年前的 wkhtmltopdf 和分页符存在很多问题,但我还没有找到解决方案.我正在使用 PDFKit gem 将我的 html 页面生成为 pdf,但我不希望页面在表格行的中间中断.
我正在使用 wkhtmltopdf-binary (0.9.9.3),它看起来是最新的版本
我的 CSS:
@media print {#scores 表 tr td, #scores 表 tr th {page-break-inside: 避免 !important;}表,tr,td,th,tbody,thead,tfoot {page-break-inside: 避免 !important;}}
我的桌子:
<table id="scores" class="print-friendly"><% @chapters.each 做 |chapter|%><tr><th colspan="3" ><%=chapter.name %></th></tr><%chapter.rules.each do |rule|%><tr><th colspan="2" >规则:<%= rule.name %></th><th></th></tr><!-- 触发器--><% rule.triggers.each do |trigger|%><tr><td>T</td><td><%=markdown(trigger.body)%></td><td><%= markdown(trigger.explanation)%></td></tr><% if trigger.image?||触发器.image2?%><tr><td></td><% if trigger.image?%><td><%= image_tag trigger.image.url(:thumb) %></td><%其他%><td></td><%结束%><% if trigger.image2?%><td><%= image_tag trigger.image2.url(:thumb) %></td><%其他%><td></td><%结束%></tr><%结束%><%结束%><!-- 问题--><% rule.questions.each do |question|%><tr><td>Q</td><td><%= markdown(question.body)%></td><td><%= markdown(question.answer) %></td></tr><% if question.image?||问题.image2?%><tr><td></td><% if question.image?%><td><%= image_tag question.image.url(:thumb) %></td><%其他%><td></td><%结束%><% if question.image2?%><td><%= image_tag question.image2.url(:thumb) %></td><%其他%><td></td><%结束%></tr><%结束%><%结束%><!-- 提示--><% rule.hints.each do |hint|%><tr><td>H</td><td><%=markdown(hint.body)%></td><td><%= markdown(hint.explanation)%></td></tr><% ifhint.image?||提示.image2?%><tr><td></td><% ifhint.image?%><td><%= image_tag hint.image.url(:thumb) %></td><%其他%><td></td><%结束%><% ifhint.image2?%><td><%= image_tag hint.image2.url(:thumb) %></td><%其他%><td></td><%结束%></tr><%结束%><%结束%><%结束%><%结束%></tbody>是否有解决办法,或者我做错了什么?结果如下:
我也可以发布 PDFKit 代码,但这听起来像是 wkhtmltopdf 问题
***Update - 当 .pdf 添加到 url 时,我的 CSS @print 不会影响页面.我有我与媒体的样式表链接:全部"
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>真%>
这是我的初始值设定项 pdfkit.rb:
ActionController::Base.asset_host = Proc.new { |源,请求|如果 request.env["REQUEST_PATH"].include?.pdf""file://#{Rails.root.join('public')}"别的#{request.protocol}#{request.host_with_port}"结尾}
如果我能修复CSS,那么我可能会解决分页问题!
解决方案 我在 css 中添加了这个,它对我有用
@media print {#分数{分页前:总是;}}
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!
解决方案 i added this in css it worked for me
@media print {
#scores {
page-break-before: always;
}
}
这篇关于带有 pdfkit on Rails 的 WKHTMLTOPDF 忽略表格分页符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆