generating print optimized pdf documents from html and css using apache fop and css2xslfo

# Uses css2xslfo and apache fop to generate a print optimized pdf document
# from an html document and a css stylesheet. This works by converting the
# styled html to an xslfo document, which then gets translated to the final
# pdf document. For further details on apache fop and how wo write appropriate
# css stylesheets for css2xslfo, see http://xmlgraphics.apache.org/fop/ and
# http://www.re.be/css2xslfo/

module Css2fopRenderer

class PdfGenerationException < Exception; end
class HtmlGenerationException < Exception; end

# overwrite for customization
def after_html_generation; end

def css2fop(template_file, html_file, pdf_file, base_url, erb_binding = nil)
render_html(template_file, html_file, erb_binding)
after_html_generation
render_pdf(html_file, pdf_file, base_url)
end

def render_pdf(html_file, pdf_file, base_url)
success = execute_css2fop(html_file, pdf_file, base_url)
unless success && $?.exitstatus == 0 && (f = File.stat(pdf_file)) && f.size > 0
raise PdfGenerationException
end
end

def render_html(template_file, html_file, erb_binding = nil)
template = ERB.new(File.read(template_file))
File.open(html_file, "w") { |f| f.puts template.result(erb_binding) }
unless (f = File.stat(html_file)) && f.size > 0
raise HtmlGenerationException
end
end

# a shell script is used so that sysadmins can customize the fop installation
def execute_css2fop(html_file, pdf_file, base_url)
system "#{ENV['css2fop-pdf']} #{html_file} #{pdf_file} #{base_url}"
end

end


Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>