Skip to content

Instantly share code, notes, and snippets.

@jozr
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jozr/58c84fec709c534f7e36 to your computer and use it in GitHub Desktop.
Save jozr/58c84fec709c534f7e36 to your computer and use it in GitHub Desktop.
require 'prawn'
require 'prawn/table'
module Spree
class ExamplePdf < Prawn::Document
HEADER = ['I', 'II', 'III', 'IV']
WIDTH = 200
def initialize
super(page_layout: :landscape)
numbers = { 'uno' => { 'one' => [[1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4]], 'four' => [[1, 2, 3, 4]] },
'dos' => { 'one' => [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4]] },
'tres' => { 'one' => [[1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4]] },
'cuatro' => { 'one' => [[1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]},
'cinco' => { 'one' => [[1, 2, 3, 4], [1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4]] },
'seis' => { 'one' => [[1, 2, 3, 4]], 'two' => [[1, 2, 3, 4]], 'three' => [[1, 2, 3, 4]] }}
data = []
numbers.each do |spanish, elem|
table_data = []
elem.each do |english, int|
english_row = [{ content: english, colspan: 4 }]
sub_table = make_table(int.unshift(english_row), header: true, width: WIDTH)
table_data << [{ content: sub_table, colspan: 4 }]
end
spanish_row = [{ content: spanish, colspan: 4 }]
spanish_table = make_table([spanish_row] + table_data, header: true, width: WIDTH)
data << [{ content: spanish_table, colspan: 4}]
end
table([HEADER] + data, header: true, width: WIDTH)
end
end
end
__END__
pdf = Spree::ExamplePdf.new
pdf.render_file 'oonu.pdf'
save_path = Rails.root.join('/Users/YOUR_USERNAME/Desktop','oonu.pdf')
File.open(save_path, 'wb') do |file|
file << pdf.render
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment