-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfs.py
More file actions
56 lines (46 loc) · 1.47 KB
/
pdfs.py
File metadata and controls
56 lines (46 loc) · 1.47 KB
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
from flask import Flask, Response
import jinja2
import os
from jinja2 import Template
import pexpect
PDFLATEX_COMMAND = "pdflatex -interaction=nonstopmode -output-directory=tex {}"
TEMPLATE_TEX = 'tex/voterpincard.tex'
FILENAME = 'pin'
VOTERPIN_TEX = 'tex/' + FILENAME + '.tex'
VOTERPIN_PDF = 'tex/' + FILENAME + '.pdf'
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False,
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
def create_pdf(voterpin):
# Get the latex template
template = latex_jinja_env.get_template(TEMPLATE_TEX)
# Render this with the correct voter pin
pin_tex = template.render(voterpin=voterpin)
# Write pin_tex to a file
file = open(VOTERPIN_TEX,'w')
file.write(pin_tex)
file.close()
# Render it as a pdf
tex_render = pexpect.spawn(PDFLATEX_COMMAND.format(VOTERPIN_TEX))
try:
tex_render.expect("Output written on", timeout=30)
except pexpect.TIMEOUT:
return "Rendering the PDF took too long, please try again!"
# Load PDF into memory
pdf_file = open(VOTERPIN_PDF)
# Remove files from filesystem
os.remove(VOTERPIN_TEX)
os.remove(VOTERPIN_PDF)
os.remove('tex/' + FILENAME + '.aux')
os.remove('tex/' + FILENAME + '.log')
return Response(pdf_file, mimetype='application/pdf')