python - How to add multiple lines at bottom (footer) of PDF? -


i have create pdf file in need add lines @ bottom left footer.

following code working:

import stringio reportlab.pdfgen import canvas import uuid  def test(pdf_file_name="abc.pdf", pdf_size=(432, 648), font_details=("times-roman", 9)):     # create new pdf reportla      text_to_add = "i writing here.."     new_pdf = "test_%s.pdf"%(str(uuid.uuid4()))      packet = stringio.stringio()      packet.seek(0)      c = canvas.canvas(pdf_file_name, pagesize = pdf_size)     #- length of text in pdf.      text_len = c.stringwidth(text_to_add, font_details[0], font_details[1])     #- take margin 20 , 20 in both axis      #- adjust starting point on x axis according  text_len     x = pdf_size[0]-20  -  text_len     y = 20      #- set font.     c.setfont(font_details[0], font_details[1])     #- write text,     c.drawstring(x, y, text_to_add)      c.showpage()     c.save()     return pdf_file_name 

now if text have multiple lines not working because length of text greater width of page size. understood.

i try frame , paragraph still can not write text in correct position in pdf

following code:

from reportlab.lib.pagesizes import letter reportlab.lib.styles import getsamplestylesheet reportlab.platypus import basedoctemplate, frame, pagetemplate, paragraph  styles = getsamplestylesheet() stylen = styles['normal'] styleh = styles['heading1']  def footer(canvas, doc):     canvas.savestate()     p = paragraph("this multi-line footer.  goes on every page.  " * 10, stylen)     w, h = p.wrap(doc.width, doc.bottommargin)     print "w, h:", w, h     print "doc.leftmargin:", doc.leftmargin     p.drawon(canvas, 10, 30)     canvas.restorestate()  def test():     doc = basedoctemplate('test.pdf', pagesize=(432, 648))     print "doc.leftmargin:", doc.leftmargin     print "doc.bottommargin:", doc.bottommargin     print "doc.width:", doc.width       print "doc.height:", doc.height     frame = frame(10, 50, 432, 648, id='normal')     template = pagetemplate(id='test', frames=frame, onpage=footer)     doc.addpagetemplates([template])      text = []     in range(1):         text.append(paragraph("", stylen))      doc.build(text)  

not understand why size of page change, because set (432, 648) show (288.0, 504.0)

doc.leftmargin: 72.0 doc.bottommargin: 72.0 doc.width: 288.0 doc.height: 504.0 

also frame size:

w, h: 288.0 96 doc.leftmargin: 72.0 

do not know how fix issue. refer this link

first mystery regarding doc.width, doc.width isn't actual width of document. width of area between margins in case doc.width + doc.leftmargin + doc.rightmargin equals width of actual page.

now why footer did not span entire width of page wanted. because of same issue described above, namely doc.width isn't actual paper width.

assuming want footer span entire page

def footer(canvas, doc):     canvas.savestate()      p = paragraph("this multi-line footer.  goes on every page.  " * 10, stylen)      # notice letter[0] width of letter page size     w, h = p.wrap(letter[0] - 20, doc.bottommargin)      p.drawon(canvas, 10, 10)     canvas.restorestate() 

assuming want footer span width of writable area

note: margins on default setting pretty big why there empty space on sides.

def footer(canvas, doc):     canvas.savestate()     p = paragraph("this multi-line footer.  goes on every page.  " * 10, stylen)     w, h = p.wrap(doc.width, doc.bottommargin)     print "w, h:", w, h     print "doc.leftmargin:", doc.leftmargin     p.drawon(canvas, doc.leftmargin, 10)     canvas.restorestate() 

edit:

as might useful know normal text should start. need figure out height of our footer. under normal circumstances cannot use p.height depends on width of text, calling raise attributeerror.

in our case able height of footer either directly p.wrap (the h) or calling p.height after have called p.wrap.

by starting our frame @ height of footer never have overlapping text. yet important remember set height of frame doc.height - footer.height ensure text won't placed outside page.


Comments