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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| import qrcode
from barcode import EAN13
import io
from io import BytesIO
import PIL.Image as ImagePil
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 Table, TableStyle, Paragraph, Image
from reportlab.lib.styles import getSampleStyleSheet
from barcode.writer import ImageWriter
import fitz
data = ["202300001234"]
qr = qrcode.QRCode(version = 1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4)
qr.add_data(data)
qr.make(fit=True)
qr_img = qr.make_image(fill_colo="black", back_color="white")
#r_img.show()
buffer_cb = BytesIO()
my_code = EAN13(str(202300001234), writer=ImageWriter()).write(buffer_cb)
img = ImagePil.open(io.BytesIO(buffer_cb.getvalue()))
#img.show()
# 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'
J = Image(io.BytesIO(buffer_cb.getvalue()))
J.drawHeight = 10 * mm
J.drawWidth = 30 * mm
#print(J)
buffer_pdf = BytesIO()
width, height = A4
pdf_canvas2 = canvas.Canvas(buffer_pdf, pagesize=letter)
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", J, "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)])
table.setStyle(style)
table.wrapOn(pdf_canvas2, width - 20 * mm, height - 150 * mm)
table.drawOn(pdf_canvas2, 12 * mm, 150)
pdf_canvas2.save()
pdf = fitz.open(stream=buffer_pdf.getvalue(), filetype="pdf")
page = pdf[0]
pix = page.get_pixmap()
image_pdf = ImagePil.frombytes("RGB", (pix.width, pix.height), pix.samples)
image_pdf.show() |
Partager