I'm preparing a PDF report of a list of items, which essentially is a long table, with some cells filled with data and some cells empty. This report will be printed out and the user will fill the empty cells on paper. She will also add new rows and fill in new data.
So I will provide some (e.g. 3) empty rows as a grid to fill in data. But if there is still room left on the page, I'd like to fill it with empty rows.
How can I add as many rows as fit on the last page?
Different approach:
Maybe this behaviour is easier achieved when using a Paragraph with a one-row-table for each row. But I need the first row repeated on every page (which is easy in tables with repeatRows=1
).
Any ideas?
I'll have to go retrieve some of my code, but I seem to recall having to do things by measuring the current X / Y Position, calculate that against whatever margin I was using, and then determine whether or not more information could fit or if I needed a new page. My project was word wrapping long blocks of text, which is similar but not quite analogous. I'll update with some code here shortly.
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
In this case I had attributes for lpp (Lines Per Page) which may have been set, so I first checked to see if that value existed, and if so, if I was at the line count for the current page. If there was no restriction on total lines per page then I tested for what my Y position would be and what the bottom margin was. If necessary, patch in a page. There's some left out here, but that's a general idea.
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
self.addLine()
self.putText(self.x, self.h - self.y, text)
def putText(self, x, y, text):
# If we actually place some text then we want to record that.
if len(text.strip()) > 0 and not self.hasText[self.pages]:
self.hasText[self.pages] = True
# Something here to handle word wrap.
if self.wrap:
lines = self._breakScan(text)
if len(lines) > 1:
self.c.drawString(x, y, lines[0])
self.newline('', ' '.join(lines[1:]))
elif lines:
self.c.drawString(x, y, lines[0])
else:
self.c.drawString(x, y, text)
Here, self.c
is my canvas. I'm keeping track of how many lines I've put down on the page because there are times where we're re-wrapping a document that may contain page breaks, all in our custom markup.