Skip to content
Advertisement

How to construct PDF (with FPDF) so that table columns span pages?

Hey i am using fpdf in python for showing a list the, Below is my code:

JavaScript

Note: the length of header and body is always same

the column H6,H7 are out of margin, so how an i make one pdf page show 5 columns and the rest of the columns show in next page Now the pdf page looks like this Now how can i achieve that

Advertisement

Answer

We’re going to need some simple math and, most importantly, some clear ideas about how we want to print the columns/rows and the individual cells.

First, we need to decide how many rows and columns we want to print per page.

I’ll call this the “print resolution”. Our table of input data can be any size (row by height), but it must be displayed, say, in a grid no bigger than 5 rows by 10 columns per page, so our resolution is “5 rows by 10 columns per page”.

Next, we need to find the print-able area of a page to figure out how big to make those rows and columns.

FPDF tell us the overall dimensions of the page with pdf.w and pdf.h, and it also tells us the margins that are set, with pdf.t_margin (for top margin) and so on for the left (l_margin), right (r_margin), and bottom (b_margin):

  • The overall width minus the left and right margins tells us how much space we have on the page to fit the 10 columns we picked earlier.

  • The overall height minus the top and bottom margins tells us how much space we have on the page to fit the 5 rows.

Having picked our print resolution and knowing those printable dimensions we can now figure out how big to make each cell:

  • Printable-width / 10 equals the width the individual cells need to be to completely fill the grid on the page.

  • Printable-height / 5 gives us the same for the cell’s height.

So, let’s just print a grid 5 rows by 10 columns that evenly fills the page:

JavaScript

and I get:

enter image description here

I’ve decided to use pdf.ln() at the end of printing a line of cells as it doesn’t take any logic: since we’re outside the loop of columns we definitely know that we’ve finished that row and need to move to the beginning of the next line.

Now that we have a nice looking grid with a desired resolution, we can move on to dealing with a table of information that is bigger than our print resolution, and how to deal with the overflow.

I think of what we’re about to do as picking a batch of rows that fit on a page, then moving across those rows from left-to-right printing the batch of columns that fit on a page:

  1. Set the row-counter to 0
  2. Start at the row-counter of the table, and select the next number-of-rows-per-page rows.
  3. Set the column-counter to 0
  4. Start at the column-counter of the table, and select the next number-of-columns-per-page columns.
  5. Print all the cells in that range of rows and columns.
  6. Advance the column counter, and repeat steps 4 & 5 till all columns have been printed.
  7. Advance the row counter, and repeat steps 2 – 6 till all rows have been printed.
JavaScript

If I give it this table:

JavaScript

and pick the same resolution of 5 rows by 10 columns, I get the following:

enter image description here

I don’t have the time or energy to show how to handle the header, but I think a simple if i == 0 then header-style else normal-style check to see if your dealing with the first row. And, include the header with the data, don’t try to handle/print them as separate data structures (that’ll mess up the math).

I was also thinking that if you want the header on each page, check out the section on Headers, Footers in the FPDF tutorial. I’m not sure how adding a header will affect the margins, so play around and see.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement