Skip to content

Instantly share code, notes, and snippets.

@donv
Last active August 29, 2015 14:01
Show Gist options
  • Save donv/27396b54059960814423 to your computer and use it in GitHub Desktop.
Save donv/27396b54059960814423 to your computer and use it in GitHub Desktop.
Prawn rotated table cell
require 'rubygems'
require 'prawn'
module Prawn
class Table
class Cell
class Text < Cell
# Returns the width of this text with no wrapping. This will be far off
# from the final width if the text is long.
def natural_content_width
@natural_content_width ||= rotated ?
with_font do
b = text_box(:width => spanned_content_height + FPTolerance)
b.render(:dry_run => true)
b.height
end :
[styled_width_of(@content), @pdf.bounds.width].min
end
def rotated
@text_options[:rotate].to_i % 180 == 90
end
# Returns the natural height of this block of text, wrapped to the
# preset width.
def natural_content_height
rotated ? [styled_width_of(@content), @pdf.bounds.height].min :
with_font do
b = text_box(:width => spanned_content_width + FPTolerance)
b.render(:dry_run => true)
b.height + b.line_gap
end
end
# Draws the text content into its bounding box.
def draw_content
with_font do
@pdf.move_down((@pdf.font.line_gap + @pdf.font.descender)/2)
with_text_color do
text_box(:width => rotated ? spanned_content_height : spanned_content_width + FPTolerance,
:height => rotated ? spanned_content_width : spanned_content_height + FPTolerance,
:at => [2, (rotated ? (spanned_content_height + FPTolerance - natural_content_height) / 2 : @pdf.cursor) ]).render
end
end
end
end
end
end
end
File.open('table.pdf', 'w') do |f|
Prawn::Document.new do
table [
[{content: 'Rotated', rowspan: 4, rotate: 90}, 'Regular'],
['Line 1'],
['Line 2'],
['Line 3'],
]
end.render(f)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment