wicked_pdf: Is it possible to have the header only show on the first page & the footer only on the last?
Asked Answered
D

2

2

Right now I have :header => {:html => {:template => "layouts/pdf_header.html.erb"}} setting things up. Is there something I can pass in that will only show the header on the first page of the document?

Can I do this for the footer as well? It's almost more important here, because I can't really think of any other way to get a footer to dynamically stick to the bottom of the page when it's going to be different lengths and different numbers of pages every time.

Decastere answered 14/11, 2012 at 17:8 Comment(2)
Maybe if you give no header but do a render :partial => '/path/to/my/header' on the first page?Zobkiw
@Zobkiw I realize I can do that, but am hoping to not have to include header & footer partials on every page that will be rendered to pdf. I also use the views for other things besides pdf's, so don't want to have to stick logic in there as well.Decastere
H
5

Some quick javascript can take care of this. Follow the boilerplate laid out under 'Footers And Headers' on wkhtmltopdf

These are your header and footer templates respectively. The key variable to keep track of is 'page' which is available from URL hash. Your header will look something like this:

<div class="headerContent" style="display:none">
   ...my awesome html
    <hr />
</div>
<script type="text/javascript">
    var headerCheck = function() {
      var x=document.location.search.substring(1).split('&');
      for (var i in x) {
        if(x[i] == "page=1")
          document.getElementsByClassName("headerContent")[0].style.display = "block";
       }
     }();
</script>

Similarly for your footer the code will look something like this:

<div class="footerContent" style="display: none">
    ...awesome footer html
</div>
<script type="text/javascript">
    var footerCheck = function() {
       var x=document.location.search.substring(1).split('&');
       var currentPage = 1;
       for (var i in x) {
         var z=x[i].split('=',2);
         if(z[0] == "page")
           currentPage = unescape(z[1]);
         if(z[0] == "topage" && currentPage == unescape(z[1]))
           document.getElementsByClassName("footerContent")[0].style.display = "block";
        }
     }();
 </script>
Halvaard answered 3/12, 2012 at 18:24 Comment(1)
This is great, a little modification and it will work to render different headers/footers for even/odd pages :)Consol
U
-1

Add layout when rendering pdf

format.pdf do
  render :pdf => "my_pdf",
  :layout => 'pdf
end

views/layouts/pdf.haml

%html
  %head

  %body
    = render "layouts/header"
    = yield
    = render "layouts/footer"
Unsphere answered 19/12, 2014 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.