admin管理员组文章数量:1391964
This is my controller
class WeleController < 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
<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 e directly?
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..
This is my controller
class WeleController < 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
<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 e directly?
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..
Share Improve this question edited Jun 19, 2014 at 5:34 Santino 'Sonny' Corleone asked Jun 19, 2014 at 4:51 Santino 'Sonny' CorleoneSantino 'Sonny' Corleone 1,7535 gold badges28 silver badges55 bronze badges1 Answer
Reset to default 7Found the answer... I had to reinstall wkhtmltopdf. Download the latest version of the wkhtmltopdf from this site http://wkhtmltopdf/downloads.html
Next,just follow the instructions from the official site https://github./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
<html xmlns="http://www.w3/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./jquery-1.10.0.min.js" %>
<%= javascript_include_tag "http://code.jquery./ui/1.10.3/jquery-ui.min.js" %>
</head>
<body>
Add some text here .........................................................................................
</body>
</html>
footer.pdf.erb
<html xmlns="http://www.w3/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./jquery-1.10.0.min.js" %>
<%= javascript_include_tag "http://code.jquery./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>
本文标签: ruby on railsPage number using javascript on wickedpdfStack Overflow
版权声明:本文标题:ruby on rails - Page number using javascript on wickedpdf - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744733943a2622211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论