如何在Ruby on Rails 5.2中使用Grover Gem调整页脚文本大小?
问题描述
在Ruby on Rails 5.2环境中使用Grover Gem渲染PDF表单时,页脚文本过小且显示异常,尝试通过style_tag_options设置footer{font-size:16px}无效,需找到有效调整页脚字体大小的方法。
有效解决方案
1. 直接在页脚模板内嵌入内联样式
Grover的页脚模板是独立HTML片段,内联样式优先级最高,可直接覆盖默认样式。修改控制器中footer变量:
footer = "<footer style='font-size:16px; margin: 8px 0;'>#{date_time_now}</footer><footer style='font-size:14px;'>This challan is system generated. Hence, no seal is required.</footer>"
2. 自定义独立页脚模板并引入样式
若页脚内容复杂,可创建单独的模板文件管理样式:
- 创建
app/views/shared/pdf_footer.html.slim:
style .pdf-footer { font-size: 16px; line-height: 1.6; text-align: center; width: 100%; } .footer-container .pdf-footer= local_assigns[:current_time] .pdf-footer This challan is system generated. Hence, no seal is required.
- 在控制器中渲染该模板并传入变量:
footer = render_to_string( template: 'shared/pdf_footer', layout: false, locals: { current_time: date_time_now } )
3. 在页脚模板中嵌入全局样式并强制覆盖
通过在页脚模板内添加<style>标签,结合!important强制覆盖Grover默认的页脚样式:
footer = <<~HTML <style> footer { font-size: 16px !important; font-family: Arial, sans-serif; } </style> <footer>#{date_time_now}</footer> <footer>This challan is system generated. Hence, no seal is required.</footer> HTML
4. 调整Grover的底部边距配置
当前Grover配置的底部边距仅0.05in,过小的边距会挤压页脚导致文本被压缩。修改Grover全局配置:
Grover.configure do |config| config.options = { display_header_footer: true, margin: { top: '0.05in', bottom: '0.5in', # 调大底部边距,给页脚足够空间 left: '0.4in', right: '0.4in' }, # ...其他原有配置 } end
内容的提问来源于stack exchange,提问作者Nahian Rifaat
相关产品推荐
相关产品推荐

