[英]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.