-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHsCprCardGenerator.py
120 lines (97 loc) · 3.66 KB
/
HsCprCardGenerator.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
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
110
111
112
113
114
115
116
117
118
119
120
# coding: utf-8
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
def generate_pdf(
tc_name="", tc_address="", course_location="", instructor_name_id="",
issue_date="", expire_date="", test=True, child=True, infant=True,
student1_name=None, student1_address_1="", student1_address_2="",
student2_name=None, student2_address_1="", student2_address_2=""):
#create page1 mask
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=(611.52, 792))
can.setFont("Helvetica", 10)
#card 1
if student1_name is not None:
can.drawString(363, 741, tc_name)
can.drawString(363, 716, tc_address)
can.drawString(363, 692, course_location)
can.drawString(363, 668, instructor_name_id)
can.drawString(81, 690, student1_name)
can.drawString(81, 642, issue_date)
can.drawString(207, 642, expire_date)
can.drawString(81,596, student1_name)
can.drawString(81,576, student1_address_1)
can.drawString(81,556, student1_address_2)
if not child:
can.drawString(128, 655, u"✗")
if not infant:
can.drawString(185, 655, u"✗")
if not test:
can.drawString(240, 655, u"✗")
#card 2
if student2_name is not None:
can.drawString(363, 329, tc_name)
can.drawString(363, 307, tc_address)
can.drawString(363, 283, course_location)
can.drawString(363, 259, instructor_name_id)
can.drawString(81, 283, student2_name)
can.drawString(81, 231, issue_date)
can.drawString(207, 231, expire_date)
can.drawString(81,185, student2_name)
can.drawString(81,169, student2_address_1)
can.drawString(81,148, student2_address_2)
if not child:
can.drawString(128, 246, u"✗")
if not infant:
can.drawString(185, 246, u"✗")
if not test:
can.drawString(240, 246, u"✗")
can.save()
packet.seek(0)
mask = PdfFileReader(packet)
return mask
def generate_cards_with_background(pdf):
dir = os.path.realpath('.')
#cards
cards_filename = os.path.join(dir, 'pdf_templates','HS_CPR_2_card.pdf')
cards = PdfFileReader(file(cards_filename, "rb"))
#merge template with mask
merged_cards = PdfFileWriter()
page = cards.getPage(0)
page.mergePage(pdf.getPage(0))
merged_cards.addPage(page)
return merged_cards
def generate_cards_with_no_background(pdf):
merged_cards = PdfFileWriter()
page = pdf.getPage(0)
merged_cards.addPage(page)
return merged_cards
def main():
dir = os.path.realpath('.')
pdf = generate_pdf(
issue_date="5/2014",
expire_date="5/2016",
instructor_name_id="Jane Instructor 12345678901",
tc_name="Anne Arundel County FD MD05507",
tc_address="Millersville, MD 21108",
course_location="Gambrills, MD",
student1_name="John Student",
student1_address_1="123 Anystreet",
student1_address_2="Crofton, MD 21114",
student2_name="Jane Student",
student2_address_1="123 Anystreet",
student2_address_2="Crofton, MD 21114",
test=False, infant=False, child=False)
#merged_cards = generate_cards_with_background(pdf)
merged_cards = generate_cards_with_no_background(pdf)
#write pdf to file
filename = os.path.join(dir, 'test','test_HS_cards.pdf')
outputStream = file(filename, "wb")
merged_cards.write(outputStream)
outputStream.close()
if __name__ == "__main__":
main()