San*_*one 1 javascript ruby-on-rails wkhtmltopdf wicked-pdf
这是我的控制器
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
Run Code Online (Sandbox Code Playgroud)
我的视图很完美,但标题中没有任何页码对于页码我使用了这段代码
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>
Run Code Online (Sandbox Code Playgroud)
当我检查这个纯HTML我得到一个错误的x说法是null.When我研究了一下X正在采取的,我意识到,没有PARAMS被传递到URL,因此错误,我怎么传PARAMS到URL?它直接来了吗?
我试过的其他事情都是
:header => {content: render_to_string(template: 'layouts/pdf_header.html')},
:header => {content: render_to_string(layout: 'pdf_header.html')},
Run Code Online (Sandbox Code Playgroud)
但没有一个工作.我知道我可以直接使用这个获取页码
'[page] of [topage]'
Run Code Online (Sandbox Code Playgroud)
但是我想把它用于其他目的..
找到了答案......我不得不重新安装wkhtmltopdf.从这个网站http://wkhtmltopdf.org/downloads.html下载最新版本的wkhtmltopdf
接下来,请按照官方网站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
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2964 次 |
| 最近记录: |