-
Notifications
You must be signed in to change notification settings - Fork 426
Adding header and footers
Michael Pope edited this page Jul 10, 2018
·
3 revisions
Here is how I configured a dynamic header and footer
def config
PDFKit.configure do |config|
config.wkhtmltopdf = "/usr/local/bin/wkhtmltopdf"
config.default_options = {
encoding:"UTF-8",
dpi: '300',
margin_top: '30',
margin_bottom: '23',
margin_left: '10',
margin_right: '10',
header_spacing: '0',
header_html: render_header_footer("header"),
footer_spacing: '2',
footer_html: render_header_footer("footer"),
quiet: true,
:"user-style-sheet" => "#{Rails.root}/app/assets/stylesheets/pdfkit.scss"
}
end
end
# Render the footer out to a temp file
# PDFkit will only accept a file or URL, it will not accept raw text the way you expect.
# ie; passing the erb compiled output directly back to pdfkit results in non-dynamic content being displayed.
def render_header_footer(type)
compiled = ERB.new(File.read("#{Rails.root}/app/views/reports/#{type}.html.erb")).result(binding)
file = Tempfile.new(["#{type}",".html"])
file.write(compiled)
file.rewind
file.path
end
Make sure you don't have any broken links in your html files otherwise wkhtmltopdf will break tests.