Skip to content

Commit 3620a04

Browse files
committed
Handle tables contents
1 parent 235bda9 commit 3620a04

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

examples/tables.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Tables</title>
5+
</head>
6+
<body style="font-family: 'Times-Roman';">
7+
<div class="title">Tables</div>
8+
<table>
9+
<tr>
10+
<td>A 1</td>
11+
<td>A 2</td>
12+
<td>A 3</td>
13+
</tr>
14+
<tr>
15+
<td>B 1</td>
16+
<td>B 2</td>
17+
<td>B 3</td>
18+
</tr>
19+
<tr>
20+
<td>C 1</td>
21+
<td>C 2</td>
22+
<td>C 3</td>
23+
</tr>
24+
</table>
25+
</body>
26+
</html>

examples/tables.pdf

3.97 KB
Binary file not shown.

lib/prawn_html/context.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ class Context < Array
77
}.freeze
88

99
attr_reader :previous_tag
10-
attr_accessor :last_text_node
10+
attr_accessor :last_text_node, :current_table
1111

1212
# Init the Context
1313
def initialize(*_args)
1414
super
15+
@current_table = nil
1516
@last_text_node = false
1617
@merged_styles = nil
1718
@previous_tag = nil

lib/prawn_html/document_renderer.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ def on_text_node(content)
6666
def render
6767
return if buffer.empty?
6868

69-
output_content(buffer.dup, context.block_styles)
69+
content = prepare_content(buffer.dup, context.block_styles)
70+
if context.current_table
71+
td_content = content.dig(:buffer, 0, :text)
72+
context.current_table.update_content(td_content)
73+
else
74+
pdf.puts(content[:buffer], content[:options], left_indent: content[:left_indent], bounding_box: content[:bounds])
75+
end
7076
buffer.clear
7177
@last_margin = 0
7278
end
@@ -95,6 +101,7 @@ def render_if_needed(element)
95101

96102
def apply_tag_close_styles(element)
97103
tag_styles = element.tag_closing(context: context)
104+
pdf.table(element.table_data) if element.is_a?(Tags::Table)
98105
@last_margin = tag_styles[:margin_bottom].to_f
99106
pdf.advance_cursor(last_margin + tag_styles[:padding_bottom].to_f)
100107
pdf.start_new_page if tag_styles[:break_after]
@@ -118,12 +125,12 @@ def prepare_text(content)
118125
@last_text = text
119126
end
120127

121-
def output_content(buffer, block_styles)
128+
def prepare_content(buffer, block_styles)
122129
apply_callbacks(buffer)
123130
left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f
124131
options = block_styles.slice(:align, :indent_paragraphs, :leading, :mode, :padding_left)
125132
options[:leading] = adjust_leading(buffer, options[:leading])
126-
pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles), left_indent: left_indent)
133+
{ buffer: buffer, options: options, left_indent: left_indent, bounds: bounds(buffer, options, block_styles) }
127134
end
128135

129136
def apply_callbacks(buffer)

0 commit comments

Comments
 (0)