6

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")
Amarth Gûl
  • 1,040
  • 2
  • 14
  • 33

2 Answers2

1

use multiceil method instead of ceil method pass first argument 0 to use available with 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')
0

Use multi_cell() instead of cell() with ln=3 and max_line_height=pdf.font_size

ln (int): Indicates where the current position should go after the call. Possible values are: 0: to the bottom right; 1: to the beginning of the next line; 2: below with the same horizontal offset; 3: to the right with the same vertical offset. Default value: 0.

max_line_height (int): optional maximum height of each sub-cell generated

Below is a working example: Note: I had to change spacing = 3. This needs to be adjusted according to number of lines in a single cell.


from fpdf import FPDF


def test_pdf():
    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
    spacing = 3
    print(col_width)
    row_height = pdf.font_size
    for row in item_data:
        for item in row:
            pdf.multi_cell(col_width, row_height * spacing, txt=item, border=1, ln=3, max_line_height=pdf.font_size)
        pdf.ln(row_height * spacing)
    pdf.output("stack1.pdf")


if __name__ == '__main__':
    test_pdf()


I had similar problem and found the solution in the answer here Make text wrap in a cell with FPDF?

Note: I could not find python specific documentation of FPDF. I generally refer the FPDF's PHP documentation: http://www.fpdf.org/en/doc/multicell.htm http://www.fpdf.org/en/tutorial/tuto3.htm

Meritor
  • 166
  • 11