1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| from io import BytesIO
from reportlab.pdfgen import canvas
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import mm
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Image
from reportlab.lib.styles import getSampleStyleSheet
from barcode.writer import ImageWriter
import barcode
# Créer un objet BytesIO pour stocker le PDF en mémoire
buffer = BytesIO()
styleSheet = getSampleStyleSheet()
I = Image("forward.gif")
I.drawHeight = 20*mm
I.drawWidth = 20*mm
print(I)
number = '230000123456'
buffer0 = BytesIO()
barcode_image = barcode.get("Code128", number, writer=ImageWriter())
barcode_image.write(buffer0)
J = Image(buffer0.getvalue())
J.drawHeight = 10*mm
J.drawWidth = 30*mm
print(J)
pdf_canvas2 = canvas.Canvas("test.pdf", pagesize=letter)
width, height = A4
# un texte long...
multilignes = """<p style="text-align:center">
Cette ligne sera centrée.<br />
Ainsi que cette ligne.<br />
Cette ligne sera centrée.<br />
Ainsi que cette ligne.<br />
Cette ligne sera centrée.<br />
Ainsi que cette ligne.<br />
Cette ligne sera centrée.<br />
Ainsi que cette ligne.<br />
Et puis celle là aussi...</p>"""
#essai d'instance d'un texte pour simplifier le chargement de la data
P0 = Paragraph(multilignes, styleSheet["BodyText"])
# Création de la data qui s'intégrera à la table
data = [["W", "X", "Y", "Z", "Z", "Z", I, "Z"],
["", "", "", "", "", "", "", ""],
['A', "", P0, "", "", "", "", ""],
['B', "", "", "", "", "", "", ""],
['C', "", "", "", "", "", "", ""],
['D', "", "", "", "", "", "", ""],
['E', "", "", "", "", "", "", ""],
['F', "", "", "", "", "", "", ""],
['G', "", "", "", "", "", "", ""],
["H", "", ""]]
# Création de la table
table = Table(data)#, colWidths=[45*mm, 100*mm,45*mm], rowHeights=[10*mm, 10*mm, 10*mm, 10*mm])
# Définition d'un style à la table
style = TableStyle([('BACKGROUND', (0, 0), (-1, 0), '#77ccef'),
('TEXTCOLOR', (0, 0), (-1, 0), colors.pink),
('SPAN', (0, 1), (1, 1)),
('SPAN', (2, 1), (3, 1)),
('SPAN', (4, 1), (5, 1)),
('SPAN', (6, 1), (7, 1)),
('SPAN', (0, 2), (1, 2)),
('SPAN', (2, 2), (5, 2)),
('SPAN', (6, 2), (7, 2)),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('ALIGN', (1, 1), (1, 1), 'RIGHT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('LINEBELOW', (0,-1), (-1,-1), 1, colors.lightgreen),
('INNERGRID', (0, 0), (-1, -1), 1, colors.green),
('BOX', (0, 0), (-1, -1), 3, colors.black)])
# affectation du style à la table
table.setStyle(style)
# positionne et dessine le tableau sur le Canvas
table.wrapOn(pdf_canvas2, width-20*mm, height-150*mm)
table.drawOn(pdf_canvas2, 12*mm, 150)
# Sauvegarde du PDF dans l'objet BytesIO
pdf_canvas2.save()
# Affichage du pdf à partir du buffer...
pdf_canvas2.showPage() |
Partager