我正在使用邪恶的PDF gem为我的应用程序生成PDF。我的控制器中有一些逻辑,一段时间以来一直生成带有页脚和页码的PDF。
我现在需要在我的模型中生成这个PDF,并且由于移动控制器将pdf代码渲染到模型,因此它没有呈现页脚或适当的边距。
这是我的控制器中的代码:
WickedPdf.new.pdf_from_string(
render :pdf => "#{@user.id}_#{@paper.data["statementDate"]}",
:template => "statements/monthly_statement_templates/#{@paper_template}",
:footer => { :html => { template: 'statements/monthly_statement_templates/footer.pdf.erb' }},
:title => "#{@paper.data["statementDate"]} Statement",
:locals => { :statement => @paper.data },
:margin => {
:bottom => 20,
:top => 20
}
我已将代码移至模型中。它看起来如下:
av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
# Need these in case the view constructs any links or references any helper methods.
av.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end
statement_pdf = WickedPdf.new.pdf_from_string(
av.render :pdf => "#{statement.id}",
:template => "statements/monthly_statement_templates/#{statement_template}",
:footer => { :html => { :template => 'statements/monthly_statement_templates/shared/footer.pdf.erb' }},
:locals => { :statement => statement.data },
:margin => { :bottom => 20, :top => 20 }
)
比较两个PDF时,模板中的所有数据和样式都是相同的。但是,从模型渲染PDF时,它不包括具有一些文本和页码的页脚。
我的页脚如下:
<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" />
<%= stylesheet_link_tag "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" %>
<style type="text/css">
.legal { font-size: 8px; }
</style>
<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()">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="col-xs-9 legal">
</div>
<div class="col-xs-3 pull-right legal">
<small>Page <span class="page"></span> of <span class="topage"></span></small>
</div>
</div>
</div>
</body>
</html>