-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol_gen.py
68 lines (51 loc) · 2.68 KB
/
protocol_gen.py
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
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.colors import gray
from flask import send_file, make_response
from reportlab.lib.units import inch
from datetime import datetime
import tempfile
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
def generate_pdf(model_laptop, serial_number, worker, type, protocolid, charger_status):
current_date = datetime.now().strftime("%d/%m/%y")
if type == "receiving":
sentence = f"Ja {worker} odbieram poniższy sprzęt: "
header = "Protokół odbiorczy"
elif type == "delivery":
sentence = f"Ja {worker} zdaję poniższy sprzęt: "
header = "Protokół zdawczy"
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file:
pdf_name = temp_file.name
c = canvas.Canvas(pdf_name, pagesize=letter)
font_path = "DejaVuSans.ttf"
pdfmetrics.registerFont(TTFont("DejaVuSans", font_path))
c.setFont("DejaVuSans", 25)
c.drawCentredString(4.25 * inch, 10.5 * inch - 0.5 * inch, header)
c.setFont("DejaVuSans", 13)
c.drawString(1 * inch, 10.5 * inch - 1.75 * inch, sentence)
c.drawString(1 * inch, 10.5 * inch - 2.25 * inch, f"Model: {model_laptop}")
c.drawString(1 * inch, 10.5 * inch - 2.5 * inch, f"Numer Seryjny: {serial_number}")
c.drawString(1 * inch, 10.5 * inch - 2.75 * inch, f"Data: {current_date}")
wydajacy_x = 1.5 * inch - c.stringWidth("Przyjmujący:", "DejaVuSans", 13) / 2
wydajacy_y = 1.5 * inch
odbiorca_x = letter[0] - c.stringWidth("Odbierający:", "DejaVuSans", 13) - 1.5 * inch
odbiorca_y = 1.5 * inch
c.drawString(wydajacy_x, wydajacy_y, "Zdający:")
c.drawString(odbiorca_x, odbiorca_y, "Odbierający:")
c.setDash(1, 2)
c.line(wydajacy_x, wydajacy_y - 0.25 * inch, wydajacy_x + 1.5 * inch, wydajacy_y - 0.25 * inch)
c.line(odbiorca_x, odbiorca_y - 0.25 * inch, odbiorca_x + 1.5 * inch, odbiorca_y - 0.25 * inch)
date_line_y = 10.5 * inch - 3 * inch
c.line(1 * inch, date_line_y, letter[0] - 1 * inch, date_line_y)
if charger_status == "1":
c.setFont("DejaVuSans", 13)
c.drawString(1 * inch, date_line_y - 0.5 * inch, "*ładowarka")
c.setFillColor(gray)
c.setFont("DejaVuSans", 10)
c.drawCentredString(4.25 * inch, 0.5 * inch, "Sprzęt pozostaje własnością TelForceOne S.A")
c.showPage()
c.save()
response = make_response(send_file(pdf_name, as_attachment=True))
response.headers["Content-Disposition"] = f"attachment; filename={protocolid}.pdf"
return response