Reputation: 6555
I currently have the following code to try and place specific text at the start of each of my pages. The specific text being title
which is passed to this partial I'm showing.
documents/clubs/advances/player_evaluation_groups.html.erb
<h2><%= title %></h2>
<% players.each do player %>
<% if player.position_group != current_position_group %>
<div class="page-break"></div>
<h2><%= title %></h2>
<% end %>
<%= render partial: "documents/clubs/advances/player_evaluation", locals: { player: player, game: advance.pro_game } %>
<% end %>
I want the title
to show up on every page. Currently this code works as long as the player evaluations stay within one page. Otherwise they only show up at the top of the page every time a new player.position_group
changes it's value. How can I detect when wicked_pdf starts a new page so that I can just have it print for each new page?
I was going to do this by providing html for a header, as I already have it in place, but I'm not sure how to update the title if I do it that way. Currently I just show a static string for my document in the header html I provide for my document.
Upvotes: 1
Views: 1311
Reputation: 625
EDIT:
So, the way i see it, there are two possibilities.
Don't use the header provided by wicked_pdf and simulate it with the body contents. You will have to provide your own header repeating logic per page, plus you will need to manually calculate when to page break. A page break will happen when:
a) player.position_group
changes or...
b) number of player records exceeds a certain amount, given that each player record evaluation content has fixed length (all records takes the same space). If player records evaluation are of variable length, then you will need to pre-calculate it, and accumulate it so when a certain threshold is surpassed, a page break is output and the accumulator is reset to 0.
Having said that, i would group player records by position_group with #group_by
, and then if knowing ahead how many player records fits per page, use each_slice
on the amount of players that fits, so on each iteration of the each_slice
method, you output a page break and render a header. If you don't know ahead how many player records fits per page, you use the other strategy and break page + header when the threshold is surpassed.
Finally, on each iteration for the #group_by
method, you output a page break + header also.
I just used this method since i also needed to have different footers or headers in the same pdf document, so i know it works. Group the collection by position_group and for each group... create a temp pdf file with the advanced method of wicked_pdf gem WickedPdf.new.pdf_from_string
, using the standard header and footer templates. When you have all your temp pdf files generated, combine them in a single pdf file with the gem: combine_pdf
Upvotes: 2