Skip to content

Instantly share code, notes, and snippets.

@mhuggins
Last active December 31, 2015 04:49
Show Gist options
  • Save mhuggins/7936780 to your computer and use it in GitHub Desktop.
Save mhuggins/7936780 to your computer and use it in GitHub Desktop.
Generate graphs in PDFs

The libtiff steps might not be necessary, but to get everything working, I did:

brew install libtiff
brew install ghostscript
brew install imagemagick --with-libtiff
source 'https://rubygems.org'
gem 'prawn'
gem 'gruff'
Bundler.require(:default)
g = Gruff::Line.new
g.title = 'My Graph'
g.data('Apples', [1, 2, 3, 4, 4, 3])
g.data('Oranges', [4, 8, 7, 9, 8, 9])
g.data('Watermelon', [2, 3, 1, 5, 6, 8])
g.data('Peaches', [9, 9, 10, 8, 7, 9])
g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
Prawn::Document.generate('example.pdf') do |pdf|
pdf.text 'Graph Example'
pdf.image StringIO.new(g.to_blob), :fit => [pdf.bounds.width, pdf.bounds.height]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment