Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text wrap issue in fpdf in python

Tags:

python

I have generated on PDF file using pyfpdf module, but I noticed that the text is not aligned properly in each row.

I need to have text-wrap in the cell to adjust automatically. I tried the code below to generate the PDF file with a fixed size, but it didn't work on getting the text mixing in rows, you can check the screenshot in the comment to see the issue.

I need to adjust the table column size dynamic. Please help.

code :


pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=8)

item_data = [['Item Name', 'Last clock', 'lastvalue'], ['cl Loaded Class Count', '0', '0'], ['cl Total Loaded Class Count', '0', '0'], ['cl Unloaded Class Count', '0', '0'], ['comp Name of the current JIT compiler', '0', ''], ['comp Accumulated time spent in compilation', '0', '0'], ['gc ConcurrentMarkSweep number of collections per second', '0', '0'], ['gc ConcurrentMarkSweep accumulated time spent in collection', '0', '0'], ['gc Copy number of collections per second', '0', '0'], ['gc Copy accumulated time spent in collection', '0', '0'], ['gc MarkSweepCompact number of collections per second', '0', '0'], ['gc MarkSweepCompact accumulated time spent in collection', '0', '0'], ['gc PS MarkSweep number of collections per second', '0', '0'], ['gc PS MarkSweep accumulated time spent in collection', '0', '0'], ['gc PS Scavenge number of collections per second', '0', '0'], ['gc PS Scavenge accumulated time spent in collection', '0', '0'], ['gc ParNew number of collections per second', '0', '0'], ['gc ParNew accumulated time spent in collection', '0', '0'], ['mem Heap Memory committed', '0', '0'], ['mem Heap Memory max', '0', '0'], ['mem Heap Memory used', '0', '0'], ['mem Non-Heap Memory committed', '0', '0'], ['mem Non-Heap Memory max', '0', '0'], ['mem Non-Heap Memory used', '0', '0'], ['mem Object Pending Finalization Count', '0', '0'], ['mp CMS Old Gen committed', '0', '0'], ['mp CMS Old Gen max', '0', '0'], ['mp CMS Old Gen used', '0', '0'], ['mp CMS Perm Gen committed', '0', '0'], ['mp CMS Perm Gen max', '0', '0'], ['mp CMS Perm Gen used', '0', '0'], ['mp Code Cache committed', '0', '0'], ['mp Code Cache max', '0', '0'], ['mp Code Cache used', '0', '0'], ['mp PS Old Gen committed', '0', '0'], ['mp PS Old Gen max', '0', '0'], ['mp PS Old Gen used', '0', '0'], ['mp PS Perm Gen committed', '0', '0'], ['mp PS Perm Gen max', '0', '0'], ['mp PS Perm Gen used', '0', '0'], ['mp Perm Gen committed', '0', '0'], ['mp Perm Gen max', '0', '0'], ['mp Perm Gen used', '0', '0'], ['mp Tenured Gen committed', '0', '0'], ['mp Tenured Gen max', '0', '0'], ['mp Tenured Gen used', '0', '0'], ['os Max File Descriptor Count', '0', '0'], ['os Open File Descriptor Count', '0', '0'], ['os Process CPU Load', '0', '0'], ['jvm Uptime', '0', '0'], ['jvm Name', '0', ''], ['jvm Version', '0', ''], ['th Daemon Thread Count', '0', '0'], ['th Peak Thread Count', '0', '0'], ['th Thread Count', '0', '0'], ['th Total Started Thread Count', '0', '0']]


col_width = pdf.w / 3.25
print(col_width)
row_height = pdf.font_size
for row in item_data:
    for item in row:
        spacing=1.5
        pdf.cell(col_width, row_height*spacing,txt=item, border=1)
    pdf.ln(row_height*spacing)
pdf.output("stack.pdf")
like image 533
ashok valakatla Avatar asked Jul 01 '26 17:07

ashok valakatla


1 Answers

Use the multi_cell method instead of cell. Pass 0 as the first argument to use the available width of the webpage.

from fpdf import FPDF

pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.set_font('Arial', 'B', 12)
pdf.multi_cell(0, 5, 'THE EASYLEARN ACADEMY',0,1)
pdf.multi_cell(0, 5, 'Surbhi Mall, 105, Eva, Waghawadi Rd., opp. akshwarwadi temple, Hill Drive, Bhavnagar, Gujarat 364002', 0, 1)
pdf.output('A3.pdf', 'F')
like image 163
The EasyLearn Academy Avatar answered Jul 04 '26 07:07

The EasyLearn Academy