mirror of
https://github.com/lloeki/tilt-pdf.git
synced 2025-12-06 10:34:41 +01:00
header + footer support
This commit is contained in:
parent
f09e3f5247
commit
6623656530
1 changed files with 41 additions and 7 deletions
|
|
@ -13,9 +13,22 @@ module Tilt
|
||||||
def evaluate(scope, locals, &block)
|
def evaluate(scope, locals, &block)
|
||||||
main = render_html(main_html_file, scope, locals, &block)
|
main = render_html(main_html_file, scope, locals, &block)
|
||||||
|
|
||||||
render_to_tmp(*css_files, scope, locals, block) do |tmp|
|
render_to_tmp(*aux_files, scope, locals, block) do |tmp|
|
||||||
kit = PDFKit.new(main, pdfkit_options)
|
opts = pdfkit_options
|
||||||
|
|
||||||
|
if header
|
||||||
|
htmp = tmp.select { |_, f| f =~ /#{File.basename header}/ }.first[1]
|
||||||
|
opts.merge!('header-html' => htmp) if header
|
||||||
|
end
|
||||||
|
|
||||||
|
if footer
|
||||||
|
ftmp = tmp.select { |_, f| f =~ /#{File.basename footer}/ }.first[1]
|
||||||
|
opts.merge!('footer-html' => ftmp) if footer
|
||||||
|
end
|
||||||
|
|
||||||
|
kit = PDFKit.new(main, opts)
|
||||||
tmp.each { |t, f| kit.stylesheets << f if t == 'text/css' }
|
tmp.each { |t, f| kit.stylesheets << f if t == 'text/css' }
|
||||||
|
|
||||||
@output = kit.to_pdf
|
@output = kit.to_pdf
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -32,6 +45,26 @@ module Tilt
|
||||||
@config = (YAML.load(data) || {})
|
@config = (YAML.load(data) || {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def aux_files
|
||||||
|
files = css_files
|
||||||
|
files << header if header
|
||||||
|
files << footer if footer
|
||||||
|
|
||||||
|
files
|
||||||
|
end
|
||||||
|
|
||||||
|
def header
|
||||||
|
if (f = config['header'])
|
||||||
|
absolutize(f)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def footer
|
||||||
|
if (f = config['footer'])
|
||||||
|
absolutize(f)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def main_html_file
|
def main_html_file
|
||||||
main_html_from_config || find_html
|
main_html_from_config || find_html
|
||||||
end
|
end
|
||||||
|
|
@ -77,18 +110,19 @@ module Tilt
|
||||||
noop = %w[html css]
|
noop = %w[html css]
|
||||||
|
|
||||||
css = files.map do |file|
|
css = files.map do |file|
|
||||||
if noop.include?(ext = File.extname(file).sub(/^\./, ''))
|
ext = File.extname(file).sub(/^\./, '')
|
||||||
|
if noop.include?(ext)
|
||||||
["text/#{ext}", file]
|
["text/#{ext}", file]
|
||||||
else
|
else
|
||||||
tmp = Tempfile.new(File.basename(file))
|
|
||||||
tmps << tmp
|
|
||||||
template = Tilt.new(file)
|
template = Tilt.new(file)
|
||||||
|
mime = template.class.default_mime_type
|
||||||
|
ext = mime.split('/').last
|
||||||
|
tmp = Tempfile.new([File.basename(file), '.' + ext])
|
||||||
|
tmps << tmp
|
||||||
rendered = template.render(scope, locals, &block)
|
rendered = template.render(scope, locals, &block)
|
||||||
tmp.write(rendered)
|
tmp.write(rendered)
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
mime = template.class.default_mime_type
|
|
||||||
|
|
||||||
[mime, tmp.path]
|
[mime, tmp.path]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue