Skip to content

Commit 95f657e

Browse files
committed
Handle tables contents
1 parent a50f169 commit 95f657e

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-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: 9 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
@@ -118,12 +124,12 @@ def prepare_text(content)
118124
@last_text = text
119125
end
120126

121-
def output_content(buffer, block_styles)
127+
def prepare_content(buffer, block_styles)
122128
apply_callbacks(buffer)
123129
left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f
124130
options = block_styles.slice(:align, :indent_paragraphs, :leading, :mode, :padding_left)
125131
options[:leading] = adjust_leading(buffer, options[:leading])
126-
pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles), left_indent: left_indent)
132+
{ buffer: buffer, options: options, left_indent: left_indent, bounds: bounds(buffer, options, block_styles) }
127133
end
128134

129135
def apply_callbacks(buffer)

0 commit comments

Comments
 (0)