Reputation: 1753
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
<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?
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..
Upvotes: 1
Views: 4374
Reputation: 1753
Found the answer... I had to reinstall wkhtmltopdf. Download the latest version of the wkhtmltopdf from this site http://wkhtmltopdf.org/downloads.html
Next,just follow the instructions from the official site 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
<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>
Upvotes: 7