-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multi-lines text in table cells with write_html #91
Comments
As contribution, I modified the write-table-example such that the whole row is increased in height: data = (
("First name", "Last name", "Age", "City"),
("Jules", "Smith", "34", "San Juan"),
("Mary", "Ramos\n \n Ramos \n Ramos", "45", "Orlando"),
("Lucas", "Cimon", "Saint-Mahturin-sur-Loire - it may even be so long that multiple lines are needed to write it down completely", "49"),
("Carlson", "Banks", "19", "Los Angeles"),
)
line_height = pdf.font_size * 1.5
col_width = pdf.epw /6 # distribute content evenly
for row in data:
row_height_lines = 1
lines_in_row = []
for datum in row: # determine height of highest cell
output = pdf.multi_cell(col_width, line_height, datum, border=1, ln=3, split_only=True)
lines_in_row.append(len(output))
if len(output) > row_height_lines:
row_height_lines = len(output)
for tlines , datum in zip(lines_in_row, row):
# here you can hack-in the
text =datum.rstrip('\n') + (1 + row_height_lines - tlines) * '\n'
pdf.multi_cell(col_width, line_height, text, border=1, ln=3)
pdf.ln(row_height_lines * line_height) |
Thank you for sharing this code snippet @RubendeBruin Yes, I think this is a good start, and this approach could be used in |
Hi @Lucas-C , I've stated to implement this. Only the text is quite straight forward with multi_cell. Adding the borders and background is more tricky as you need to know the height of the cell which may be dictated by unprocessed cells right of it. To draw the borders and cell backgrounds you need to parse the whole row. Most logical implementation I think is to build a dataset with row data and process that when handle_endtag encounters a
Would this make sense or is there an easier way? |
Status update: PRs attempts have been closed, this issue is open for contributions! |
Since #703 has been released,multi-lines content in cells is now supported! |
Example HTML to use:
The text in the first cell of the first row currently overflows.
A starting point would be to add a dedicated test in test_html.py.
In terms of code, some changes could be made in
HTML2FPDF.handle_data
in order to callself.pdf.multi_cell(... ln=3)
instead ofFPDF.cell
. Inspiration could be taken from this test: https://github.com/PyFPDF/fpdf2/blob/master/test/cells/test_multi_cell.py#L85 Corresponding output: https://github.com/PyFPDF/fpdf2/blob/master/test/cells/multi_cell_ln_3_table.pdfThis issue can count as part of hacktoberfest
The text was updated successfully, but these errors were encountered: