Skip to content

Commit 30cdef3

Browse files
author
Olivier Bonnaure
committed
feat: handle multilines in table cells
1 parent c0bac84 commit 30cdef3

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

.lua/pdfgenerator.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function PDFGenerator:addParagraph(text, options)
520520
self.last_font = self.last_font or {}
521521
self.last_font.fontWeight = options.fontWeight
522522

523-
splittedText = string.split(text, "\n")
523+
local splittedText = string.split(text, "\n")
524524

525525
for _, text in ipairs(splittedText) do
526526
local lines = self:splitTextToLines(text, options.fontSize, options.width - options.paddingX)
@@ -585,17 +585,23 @@ function PDFGenerator:calculateMaxHeight(items)
585585
local padding_y = item.padding_y or self.current_table.padding_y or 5
586586

587587
-- Split text into lines considering the available width
588-
local lines = self:splitTextToLines(text, fontSize, width - (padding_x * 2))
588+
local lines = string.split(text, "\n")
589+
local number_of_lines = 0
590+
591+
for _, line in ipairs(lines) do
592+
local splitted_text = self:splitTextToLines(line, fontSize, width - (padding_x * 2))
593+
number_of_lines = number_of_lines + #splitted_text
594+
end
589595

590596
-- Calculate height for this item
591-
local line_height = fontSize * 1.5 -- Standard line height
592-
local text_height = #lines * line_height + (padding_y * 2) -- Include padding
597+
local line_height = fontSize * 1.5 - (number_of_lines / 1.5) -- Standard line height
598+
local text_height = number_of_lines * line_height + (padding_y * 2) -- Include padding
593599
-- Update max_height if this item is taller
594600
if text_height > max_height then
595601
max_height = text_height
596602
end
597-
item.lines = #lines
598-
item.height = #lines * line_height
603+
item.lines = number_of_lines
604+
item.height = number_of_lines * line_height
599605
end
600606

601607
self.current_table.current_row.height = max_height
@@ -625,9 +631,9 @@ function PDFGenerator:drawRowTable(columns, row_options)
625631
for _, column in ipairs(columns) do
626632
-- Draw the cell using existing method
627633
local options = table.merge(row_options, column)
628-
options.text = nil
629-
options.height = column.height
630-
self:drawTableCell(column.text, options)
634+
-- options.text = nil
635+
-- options.height = column.height
636+
self:drawTableCell(column.text , options)
631637
end
632638

633639
-- Reset cursor position
@@ -671,7 +677,7 @@ function PDFGenerator:drawTableCell(text, options)
671677
self:moveX(-self.current_table.padding_x)
672678
end
673679

674-
self:moveY(self.current_table.padding_y)
680+
self:moveY(self.current_table.padding_y - 1)
675681

676682
local paddingY = 0
677683
if options.vertical_alignment == "middle" then

app/controllers/welcome_controller.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ local app = {
7070
}
7171

7272
local dataColumns = {}
73-
for i = 1, 3 do
73+
for i = 1, 10 do
7474
table.insert(dataColumns, {
75-
{ text = "demo " .. i, width = 305, fontSize = 10, alignment = "left", borderSides = { right = "false" } },
75+
--{ text = "demo " .. i, width = 305, fontSize = 10, alignment = "left", borderSides = { right = "false" } },
76+
{
77+
text = "Demo" .. i,
78+
width = 305, fontSize = 10, alignment = "left",
79+
borderSides = { right = "false" }
80+
},
7681
{ text = "$700", width = 70, fontSize = 10, alignment = "right", borderSides = { left = "false", right = "false" } },
7782
{ text = "2", width = 50, fontSize = 10, alignment = "center", borderSides = { left = "false", right = "false" } },
7883
{ text = "$1400", width = 70, fontSize = 10, alignment = "right", borderSides = { left = "false" } }
@@ -91,7 +96,7 @@ local app = {
9196
local dataColumns = {}
9297
for i = 1, 10 do
9398
table.insert(dataColumns, {
94-
{ text = "Pro Morbi ultrices pharetra risus sed pellentesque. Integer id semper erat. Duis l" .. i, width = 305, fontSize = 10, alignment = "left" },
99+
{ text = "Pro Morbi ultrices\npharetra risus sed pellentesque.\nInteger id semper erat. Duis l" .. i, width = 305, fontSize = 10, alignment = "left" },
95100
{ text = "$700", width = 70, fontSize = 10, alignment = "right", vertical_alignment = "middle" },
96101
{ text = "2", width = 50, fontSize = 10, alignment = "center", vertical_alignment = "middle" },
97102
{ text = "$1400", width = 70, fontSize = 10, alignment = "right", vertical_alignment = "middle" }
@@ -103,7 +108,7 @@ local app = {
103108
data_columns = dataColumns,
104109
header_options = { fillColor = "000", borderColor = "000", textColor = "fff" },
105110
data_options = { fillColor = "fff", borderColor = "eee", oddFillColor = "fafafa", evenFillColor = "fff" }
106-
}, { padding_x = 5, padding_y = 2 })
111+
}, { padding_x = 5, padding_y = 5 })
107112

108113
for i = 1, 1 do
109114
pdf:moveY(10)

0 commit comments

Comments
 (0)