-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
208 lines (192 loc) · 6.88 KB
/
generator.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2019 Håkan Olsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from jinja2 import Environment
from PyPDF2 import PdfFileWriter, PdfFileReader
import json
import pdfkit
import cheatSheet
import sys
import traceback
frontTemplate = """
<!DOCTYPE html>
<html>
<head>
<title>Game</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body class="main">
<div style="text-align:center;">
{% for cardType in cardTypes -%}
{% for card in cardType.cards -%}
{% for x in range(card.count) -%}
<div class="box box-card" style="border: 1px solid white;">
<div class="inner" style="width:100%; height: 100%;">
<div style="width:100%; height: 100%; display: table; background-image: url({%if card.frontImage %}{{card.frontImage}}{% else %}{{cardType.frontImage}}{% endif %}); background-repeat:no-repeat; background-position: center; background-size: 80%;">
<div class="inner-card" style="width:100%; height: 100%;">
<table style="width:100%; height: 100%; margin: 0px;">
<tr style="height: 100%; vertical-align: top;">
<td>
<h4>{{ card.title }}</h4>
<p>{{ card.description }}</p>
{% for attribute in card.attributes -%}
<h5>{{ attribute.title }}</h5>
<p>{{ attribute.description }}</p>
{% endfor -%}
{% if cardType.template -%}
<br>
{%for x in range(cardType.template|length)-%}
<p><b>{{cardType.template[x]}}:</b> {{card['values'][x]}}</p>
<br>
{% endfor -%}
{% endif -%}
{% if cardType.countText -%}
<p><b>{{ cardType.countText }}</b> {{card.count}}</p>
{% endif -%}
</td>
</tr>
{% if card.turns -%}
<tr style="height: 0;">
<td>
<table style="width:100%; text-align: center;">
<tr>
{% for t in range(card.turns) -%}
<td><div class="circle"><span>{{t+1}}</span></div></td>
{% endfor -%}
</tr>
</table>
</td>
</tr>
{% endif -%}
{% if card.summation -%}
<tr style="height: 0;">
<td>
<p class="summation">{{ card.summation }}</p>
</td>
</tr>
{% endif -%}
</table>
</div>
</div>
</div>
</div>{% endfor -%}
{% endfor -%}
<br>
{% endfor -%}
{% for t in range(numerOfCheatSheet) -%}{{ cheatSheet }}{% endfor -%}
</div>
</body>
</html>
"""
backTemplate = """
<!DOCTYPE html>
<html>
<head>
<title>Game</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body class="main">
<div style="text-align:center;">
{% for cardType in cardTypes -%}
{% for card in cardType.cards -%}
{% for x in range(card.count)-%}
<div class="box box-card" style="border: 1px solid black;">
<div class="inner inner-card" style="display: table;">
<h4 style="display: table-row;">{{ cardType.name }}</h4>
<div style="display: table-row; height:100%;">
<table style="height:100%; vertical-align: middle;">
{% if cardType.backImages|length == 3 -%}
<tr>
<th><img src={{cardType.backImages[0]}} style="max-width:90%; height: auto;"></th>
<th><img src={{cardType.backImages[1]}} style="max-width:90%; height: auto;"></th>
</tr>
<tr>
<td colspan = "2"><img src={{cardType.backImages[2]}} style="max-width:45%; height: auto;"></td>
</tr>
{% elif cardType.backImages|length == 2 -%}
<tr>
<th><img src={{cardType.backImages[0]}} style="max-width:90%; height: auto;"></th>
<th><img src={{cardType.backImages[1]}} style="max-width:90%; height: auto;"></th>
</tr>
{% elif cardType.backImages|length == 1 -%}
<tr>
<th><img src={{cardType.backImages[0]}} style="max-width:90%; height: auto;"></th>
</tr>
{% endif -%}
</table>
</div>
</div>
</div>{% endfor -%}
{% endfor -%}
<br>
{% endfor -%}
{% for t in range(numerOfCheatSheet) -%}{{cheatSheet}}{% endfor -%}
</div>
</body>
</html>
"""
options = {
'enable-local-file-access': "",
'margin-top': '15px',
'margin-right': '15px',
'margin-bottom': '15px',
'margin-left': '15px'
}
numerOfCheatSheet = 4
def debug(text):
print(text)
return ''
def read_json_file():
with open('data.json','r',encoding='utf8') as json_file:
data = json.load(json_file)
environment = Environment()
environment.filters['debug']=debug
frontPages = environment.from_string(frontTemplate).render(cardTypes=data, cheatSheet=cheatSheet.cheatSheetFront, numerOfCheatSheet=numerOfCheatSheet)
with open('fronts.html', 'w', encoding='utf8') as outfile:
outfile.write(frontPages)
backPages = Environment().from_string(backTemplate).render(cardTypes=data, cheatSheet=cheatSheet.cheatSheetBack, numerOfCheatSheet=numerOfCheatSheet)
with open('backs.html', 'w', encoding='utf8') as outfile:
outfile.write(backPages)
def pdf_from_html():
path_wkthmltopdf = r'.\lib\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_file('fronts.html', 'fronts.pdf', options=options, configuration=config)
pdfkit.from_file('backs.html', 'backs.pdf', options=options, configuration=config)
pdfkit.from_file('instructions.html', 'instructions.pdf', options=options, configuration=config)
def pdf_mix_it_up():
output = PdfFileWriter()
fronts = PdfFileReader(open("fronts.pdf", "rb"))
backs = PdfFileReader(open("backs.pdf", "rb"))
if not fronts.getNumPages() == backs.getNumPages():
raise AssertionError()
print("Front and back html documents has %d sides each." % fronts.getNumPages())
for index in range(fronts.getNumPages()):
output.addPage(fronts.getPage(index))
output.addPage(backs.getPage(index))
outputStream = open("cards.pdf", "wb")
output.write(outputStream)
if __name__ == '__main__':
read_json_file()
pdf_from_html()
pdf_mix_it_up()