0

I am currently querying data from a DB, pulling it into lists in Python and then iterating through those lists in order to print that data into a PDF. I have read through the docs at ReportLab's PDFGen Documentation.

The problem: I am not able to jump to a new line when the current line extends beyond the right-border of the page. I need a "return."

Here's what I have right now:

x = 72 #1 inch margin from
y = 720 #10 inch margin from bottom
mypdf = canvas.Canvas(response) #new canvas called 'mypdf'
mypdf.setPageSize(letter) #set size to 8.5x11
list1 = ['item 1.1', 'item 1.2', 'item 1.3',]
list2 = ['item 2.1', 'item 2.2', 'item 2.3',]

detaillist = [ list1, list2 ]

for details in detaillist:
    for line_item in details:
        mypdf.roundRect(x-15, y, 10, 10, 2, stroke=1) #each line item has a check box
        mypdf.drawString(x, y, line_item) #write the value for each line item at x,y coordinate
        y -= 14 #x, y coordinate goes down by 14/72s of an inch
        #if the current location is within bottom 1 inch margin
        if y < 72:
            y = 720 #reset y value
            page = canvas.Canvas(response) #create a new page
            mypdf.showPage() #show the new page
    mypdf.line(x,y,x*7.5,y) #draw a line to separate sections
    y -= 17 #add a buffer below the line

mypdf.save()

Right now, this iterates through my two lists. When the content gets within the one inch bottom margin, I go to a new page. Almost all the list items fit on the page. However, if I change my list items to be super long, like:

list1 = ['item 1.1 is a sentence that drags on and continues and talks about this or that or something else but at this point it is off the side of the page', 'item 1.2', 'item 1.3',]

Then my text runs off the right-hand side of the page.

What can I do? I'd be happy to hear how you might approach this. I have an unknown number of lists to iterate through and each list has 10-20 data points, each (currently) printed on one line.

R. Steele
  • 56
  • 8

0 Answers0