exploring & testing reportlab to make PDFs with Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import mm, inch
from reportlab.lib.colors import pink, black, red, blue, green
def bodytext(canvas, textfile):
# Select unit
canvas.translate(mm, mm)
# Select font
canvas.setFont("Helvetica", 14)
# unit is point!
# Add text from document
textobject = canvas.beginText()
textobject.setTextOrigin(10*mm, 10*mm) # x, y ?
for line in textfile:
# textobject.textLine(line)
# does not move the cursor
textobject.textOut(line)
textobject.moveCursor(2*mm, 17.5) # positive value is moving the cursor down!
# move the cursor for the next line
textobject.setFillGray(0.4)
textobject.textOut('''With many apologies to the Beach Boys and anyone else who finds this objectionable''')
# draw text to canvas
canvas.drawText(textobject)
def moretext(canvas, text):
tobject = canvas.beginText()
canvas.drawText(tobject)
for line in text:
canvas.drawString(line)
page = Canvas("multipaged.pdf", pagesize=A4, bottomup=0, verbosity=1)
# flow text over pages
textfile = open('language.txt', 'r').readlines()
bodytext(page, textfile)
# add a new page
page.showPage()
# add more text
text = ['Hello you!','And hello me!','What else is there to say?']
bodytext(page, text)
# add a new page
page.showPage()
# save PDF
page.save()