简体   繁体   English

在wickedpdf上使用javascript的页码

[英]Page number using javascript on wickedpdf

This is my controller 这是我的控制器

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

I get my view perfect but without any page number in the header For page number I used this code 我的视图很完美,但标题中没有任何页码。对于页码,我使用了此代码

pdf_header.html.erb 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>

When I checked this as pure html I get an error for x saying it is null.When I researched to what x was taking in,I realised that no params are being passed to the URL and hence the error,how do I pass params to the URL?Does it come directly? 当我将其检查为纯html时,出现x表示为null的错误。当我研究x接受的内容时,我意识到没有参数传递给URL,因此错误,如何将参数传递给URL吗?直接出现吗?

Other things that I tried were 我尝试过的其他事情是

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

But none worked.I know I can get page number directly using this 但是都没有用。我知道我可以使用这个直接获取页码

'[page] of [topage]' 

but I want it for some other purpose.. 但我想要其他用途

Found the answer... I had to reinstall wkhtmltopdf. 找到了答案...我不得不重新安装wkhtmltopdf。 Download the latest version of the wkhtmltopdf from this site http://wkhtmltopdf.org/downloads.html 从此站点下载最新版本的wkhtmltopdf http://wkhtmltopdf.org/downloads.html

Next,just follow the instructions from the official site https://github.com/mileszs/wicked_pdf 接下来,只需按照官方网站上的说明进行操作即可:https://github.com/mileszs/wicked_pdf

Anyways this is my full working example 无论如何,这是我完整的工作示例

This is my controller 这是我的控制器

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

My index.html.erb 我的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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM