Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTML -> PDF with JRuby and Flying Saucer
require 'java'
require 'flying_saucer'
java_import org.xhtmlrenderer.pdf.ITextRenderer
class PDFGenerator
def self.generate(html)
new(html).generate
end
def initialize(html)
@html = html
@renderer = ITextRenderer.new
setup_fonts
end
def setup_fonts
fonts_path = File.expand_path(File.join(File.dirname(__FILE__), '../fonts/*.ttf'))
font_resolver = @renderer.getFontResolver()
Dir[fonts_path].each do |file|
font_resolver.add_font(file, true)
end
end
def generate
StringIO.new.tap do |io|
@renderer.set_document_from_string(@html)
@renderer.layout
@renderer.create_pdf(io.to_outputstream)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.