Ruby on rails 页脚html内容未以pdf格式打印;轨道
我正在使用wicked_pdf为我的rails应用程序生成pdf。 我试图在生成的pdf的页脚处呈现动态html内容 我在控制器中实现了以下代码:Ruby on rails 页脚html内容未以pdf格式打印;轨道,ruby-on-rails,ruby-on-rails-4,pdf-generation,wkhtmltopdf,wicked-pdf,Ruby On Rails,Ruby On Rails 4,Pdf Generation,Wkhtmltopdf,Wicked Pdf,我正在使用wicked_pdf为我的rails应用程序生成pdf。 我试图在生成的pdf的页脚处呈现动态html内容 我在控制器中实现了以下代码: #app/controllers/receipts_controller.rb def generate_receipt html = render_to_string(:action => 'print_receipt', :layout => false) pdf = W
#app/controllers/receipts_controller.rb
def generate_receipt
html = render_to_string(:action => 'print_receipt',
:layout => false)
pdf = WickedPdf.new.pdf_from_string(html,
:encoding => 'UTF-8',
:page_size => 'A4',
:orientation => 'Portrait',
#:footer => { :content => "This is my footer content."},
:footer => { :html => { :template => 'footer.html.erb' } } )
send_data(pdf, :filename => get_file_name, :disposition => 'attachment')
end
如果我只使用静态内容(查看上面的注释部分),那么它就可以正常工作。
但它无法从提供的模板位置加载页脚内容
我的页脚位于:
#app/views/receipts/footer.html.erb
<div class='rec-foot'> This is my footer content (this is dynamic) </div>
#app/views/receipts/footer.html.erb
这是我的页脚内容(这是动态的)
以下是我的打印模板:
#app/views/receipts/print_receipt.html.erb
<htmL>
<head>
<%= wicked_pdf_stylesheet_link_tag "wicked_pdf" %>
</head>
<body>
<!--- my pdf page content goes here -->
</body>
</html>
# app/assets/stylesheets/wicked_pdf.css
.rec-foot{
font-weight: bold;
text-align: center;
}
#app/views/receipts/print_receipt.html.erb
#app/assets/stylesheets/wicked_pdf.css
.rec英尺{
字体大小:粗体;
文本对齐:居中;
}
问题:未为动态html内容呈现页脚部分 我相信
:模板
不适合您,因为您使用的是pdf\u from\u string
(来源:附近注释掉的行。)
我们通过在app/models/pdf_builder.rb中执行类似的操作来处理:content
:
:footer: {
content: { footer_html }
}
然后定义:
def footer_html
ERB.new(pdf_template).result(binding)
end
def pdf_template
File.read("full/path/of/your/file.html.erb")
end
我们在app/models/pdf_builder.rb中初始化,并在/app/views/pdf_footer.en.html.erb中动态填充所需的任何信息您可以直接将文件添加到
rails
提供的render_to_string
方法中。
例如:页脚:{
内容:呈现到字符串('pdf\u footer.html.erb',布局:'pdf\u layout.html.erb')
}
wicked\u pdf
覆盖render\u to\u string
方法,因此它也支持其所有默认选项。是否可以尝试完整路径?e、 g.实际上不需要收据/footer.html.erb甚至app/views/receipts/footer.html.erb。因为当我提供任何无效路径时,它会将识别的路径显示到视图,如“app/views”。因此,我解释为我需要添加更多的账单/文件名。