exploring & testing reportlab to make PDFs with Python
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 

99 satır
2.4 KiB

# https://www.youtube.com/watch?v=ZDR7-iSuwkQ
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm, mm
from reportlab.lib import colors
filename = 'exploring.pdf'
documenttitle = 'Testing'
title = 'Hello Reportlab'
subtitle = 'How are you doing?'
authors = 'You and Me and Many Others too'
textlines = [
'I am testing you...',
'I am testing you...',
'I am testing you...',
'What a day, no?',
'What a day, no?',
'What a day, no?',
'What a day, no?',
'What a day, no?',
'What a day, no?',
'Now we are together.'
]
image = 'tutorial-logo.png'
def drawRuler(pdf):
pdf.setFont('Courier', 8)
pdf.setFillColor(colors.blue)
# pdf.drawString(0*mm, 0*mm, 'xy10')
# pdf.drawString(10*mm, 10*mm, 'xy10')
pdf.drawString(20*mm, 160*mm, 'x20')
pdf.drawString(40*mm, 160*mm, 'x40')
pdf.drawString(60*mm, 160*mm, 'x60')
pdf.drawString(80*mm, 160*mm, 'x80')
pdf.drawString(100*mm, 160*mm, 'x100')
pdf.drawString(5*mm, 20*mm, 'y20')
pdf.drawString(5*mm, 40*mm, 'y40')
pdf.drawString(5*mm, 60*mm, 'y60')
pdf.drawString(5*mm, 80*mm, 'y80')
pdf.drawString(5*mm, 100*mm, 'y100')
pdf.drawString(5*mm, 120*mm, 'y120')
pdf.drawString(5*mm, 140*mm, 'y140')
pdf.drawString(5*mm, 160*mm, 'y160')
pdf = canvas.Canvas(filename)
pdf.setPageSize([110*mm, 170*mm])
pdf.setTitle(documenttitle)
pdf.setAuthor(authors)
drawRuler(pdf)
# for font in pdf.getAvailableFonts():
# print(font)
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
pdfmetrics.registerFont(TTFont('stan', 'StanleySmith-Broman.ttf'))
pdfmetrics.registerFont(TTFont('concrete-reg', 'cmunorm.ttf'))
pdfmetrics.registerFont(TTFont('concrete-it', 'cmunoti.ttf'))
# Title
pdf.setFillColor(colors.black)
pdf.setFont('stan', 32)
# pdf.drawString(80, 720, title) # x, y (counting from bottom-left)
pdf.drawCentredString(60*mm, 150*mm, title) # x, y (counting from middle point string)
# Subtitle
pdf.setFillColorRGB(255, 0, 255)
pdf.setFont('concrete-it', 16)
pdf.drawCentredString(60*mm, 140*mm, subtitle)
# Line
pdf.setLineWidth(50)
pdf.line(20*mm, 100*mm, 100*mm, 115*mm) # start-x, start-y, end-x, end-y
# Text
pdf.setFont('concrete-reg', 9)
pdf.setFillColor(colors.red)
text = pdf.beginText(20*mm, 70*mm)
for line in textlines:
text.textLine(line)
pdf.drawText(text)
# Image
pdf.drawInlineImage(image, 70*mm, 40*mm, width=25*mm, height=25*mm) # x, y
# Other
pagenumber = pdf.getPageNumber()
pdf.drawCentredString(60*mm, 15*mm, str(pagenumber))
# Save
pdf.save()