在wicked_pdf上使用javascript的页码

时间:2014-06-19 04:51:28

标签: javascript ruby-on-rails wkhtmltopdf wicked-pdf

这是我的控制器

class WelcomeController < ApplicationController
  def index
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "my_pdf", # pdf will download as my_pdf.pdf
        :layout => 'pdf', # uses views/layouts/pdf.haml
        :margin => { :top => 30 },
        :header => {template: 'layouts/pdf_header.html'},
        :show_as_html => params[:debug].present? # renders html version if you set debug=true in URL
      end
    end
  end
end

我的视图很完美,但标题中没有任何页码 对于页码,我使用了此代码

pdf_header.html.erb

<html>
  <head>
    <script>
      function number_pages() {
        var vars={};
        var x=document.location.search.substring(1).split('&');
        for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
        var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
        for(var i in x) {
          var y = document.getElementsByClassName(x[i]);
          for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
        }
      }
    </script>
  </head>
  <body onload="number_pages()">
    Page <span class="page"></span> of <span class="topage"></span>
  </body>
</html>

当我将其作为纯html检查时,我得到x的错误,说它是null。当我研究x正在接受的内容时,我意识到没有params传递给URL,因此错误,如何将params传递给URL?它是直接来的吗?

我尝试过的其他事情

:header => {content: render_to_string(template: 'layouts/pdf_header.html')},
:header => {content: render_to_string(layout: 'pdf_header.html')},

但没有效果。我知道我可以直接使用这个

获取页码
'[page] of [topage]' 

但我想将它用于其他目的..

1 个答案:

答案 0 :(得分:5)

找到答案...... 我不得不重新安装wkhtmltopdf。 从此网站http://wkhtmltopdf.org/downloads.html

下载最新版本的wkhtmltopdf

接下来,只需按照官方网站https://github.com/mileszs/wicked_pdf

的说明操作即可

无论如何,这是我的全部工作实例

这是我的控制器

class PdfexampleController < ApplicationController
  def index
    WickedPdf.new.pdf_from_string(
  render :pdf => 'hello',
  :template => "pdfexample/index.html.erb",
  :margin => {:top => 36, :bottom =>45 },
  :footer => {
    :content => render_to_string(:template => 'pdfexample/footer.pdf.erb')
  }
)
  end
end

我的index.html.erb

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
    <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
  </head>
  <body>
    Add some text here .........................................................................................
  </body>
 </html>

footer.pdf.erb

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
    <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
    <script>
      function number_pages() {
        var vars={};
        var x=document.location.search.substring(1).split('&');
        for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
        var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
        for(var i in x) {
          var y = document.getElementsByClassName(x[i]);
          for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
        }
      }
    </script>

  </head>
 <body onload="number_pages()">
    Page <span class="page"></span> of <span class="topage"></span>
  </body>

</html>