def pdf_render(format, file_name, view, orientation, page_size = 'A4', footer="")
@file_name = "#{file_name} [#{date_time_now}]"
html = render_to_string({
template: view,
layout: 'pdf.html.slim'
})
style_tag_options = [
{
content: 'footer{font-size: 16px}'
}
]
options = {
format: page_size,
landscape: orientation == 'Landscape',
emulate_media: 'screen',
display_url: "#{request.protocol.to_s}#{request.env['HTTP_HOST'].to_s}",
footer_template: footer,
style_tag_options: style_tag_options
}
grover = Grover.new(
html,
options
).to_pdf
format.pdf do
send_data(grover, filename: "#{@file_name}.pdf")
end
end
这是我用来呈现表单的pdf的函数。但是页脚元素被破坏了,并且太小而无法读取。我无法使用Grovergem来增加字体大小和做任何事情,而在Ruby on Rails 5.2中,系统中到处都在使用该gem;我的控制器代码如下:
def m_6_3
add_breadcrumb 'Mushak 6.3'
render('mushaks/filters/m_6_3') && return if (params[:sale].nil? || !params[:sale][:id].present?)
add_breadcrumb 'Preview'
@sale = Sale.find(params[:sale][:id])
@service_charge_column = true if @sale.company.has_vms_api
footer = "<footer>#{date_time_now}</footer><footer>This challan is system generated. Hence, no seal is required.</footer>"
print = PrintCount.find_or_create_by(creator: @sale, action: 'm_6_3')
@copy_print = print.count > 0
if params[:format].eql? 'pdf'
@type = 'pdf'
@copy_print = increase_print_count(@sale, 'm_6_3')
else
@type = 'html'
end
if params[:commit].eql?(ApplicationRecord::PDF_BUTTON_TEXT)
redirect_to m_6_3_mushaks_path(sale: { id: @sale.id }, format: 'pdf' )
end
@sale_items = @sale.sale_items
@company = @sale.company
@sum = @sale_items
.select('sum(total) as total, sum(total_in_foreign_currency) as total_in_foreign_currency, sum(vat) as vat, sum(sd) as sd, sum(net_total) as net_total')
.first
respond_to do |format|
format.html if @type == 'html'
pdf_render format, "Mushak-6.3-#{@sale.number}", 'mushaks/m_6_3.html.slim', @company&.report_preference&.m_6_3_orientation || 'Portrait', (@company&.report_preference&.m_6_3_page_size || 'A4' if @type == 'pdf'), footer
end
end
Grover gem配置如下:
Grover.configure do |config|
config.options = {
display_header_footer: true,
margin: {
top: '0.05in',
bottom: '0.05in',
left: '0.4in',
right: '0.4in'
},
user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
viewport: {
width: 640,
height: 480
},
prefer_css_page_size: true,
bypass_csp: true,
media_features: [{ name: 'prefers-color-scheme', value: 'dark' }],
extra_http_headers: { 'Accept-Language': 'en-US' },
timeout: 0,
cache: false,
launch_args: ['--font-render-hinting=medium', '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
wait_until: 'networkidle2',
print_background: true
}
end
1条答案
按热度按时间bn31dyow1#
显然,chromium有一些默认的css类和id,可以用来正确设置页眉和页脚。 puppet 师和格罗弗的记录不是很好。所以,我在他们的存储库中发现了一个问题,在链接here中有解决方案。