wicked_pdf:是否可以将标题仅显示在第一页上?页脚只放在最后? [英] wicked_pdf: Is it possible to have the header only show on the first page & the footer only on the last?

查看:45
本文介绍了wicked_pdf:是否可以将标题仅显示在第一页上?页脚只放在最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有 :header =>{:html =>{:模板=>"layouts/pdf_header.html.erb"}} 设置.有什么我可以传递的东西只会在文档的第一页上显示标题吗?

我也可以为页脚这样做吗?在这里几乎更重要,因为我真的想不出任何其他方法来让页脚动态地粘在页面底部,因为每次都会有不同的长度和不同的页面数量.

解决方案

一些快速的 javascript 可以解决这个问题.按照 wkhtmltopdf

这些分别是您的页眉和页脚模板.要跟踪的关键变量是页面",它可以从 URL 哈希中获得.您的标题将如下所示:

<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 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] == "页面")currentPage = unescape(z[1]);if(z[0] == "topage" && currentPage == unescape(z[1]))document.getElementsByClassName("footerContent")[0].style.display = "block";}}();

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.

解决方案

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>

这篇关于wicked_pdf:是否可以将标题仅显示在第一页上?页脚只放在最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆