繁体   English   中英

在wickedpdf上使用javascript的页码

[英]Page number using javascript on wickedpdf

这是我的控制器

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接受的内容时,我意识到没有参数传递给URL,因此错误,如何将参数传递给URL吗?直接出现吗?

我尝试过的其他事情是

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

但是都没有用。我知道我可以使用这个直接获取页码

'[page] of [topage]' 

但我想要其他用途

找到了答案...我不得不重新安装wkhtmltopdf。 从此站点下载最新版本的wkhtmltopdf http://wkhtmltopdf.org/downloads.html

接下来,只需按照官方网站上的说明进行操作即可: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>

暂无
暂无

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

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