UnderCover/latty.py

62 lines
2.0 KiB
Python
Raw Normal View History

# Copyright Sage Vaillancourt 2021
import os
import subprocess
2021-07-23 23:09:08 -04:00
from dataclasses import dataclass
from flask import send_from_directory
# latex /home/sage/Documents/latex/coverLetter/temp.tex'
# dvipdf /home/sage/Documents/latex/coverLetter/temp.dvi ~/Documents/SageVaillancourt_CoverLetter.pdf'
# rm -r /home/sage/Documents/latex/coverLetter/temp*'
@dataclass
class CLData():
username: str
company: str
jobandpronoun: str
skilltypes: str
myskills: str
closingtext: str
body: str
2021-07-24 01:08:18 -04:00
def get_pairs(self):
return [
2021-07-24 01:08:18 -04:00
("username", self.username),
("company", self.company),
("jobandpronoun", self.jobandpronoun),
("skilltypes", self.skilltypes),
("myskills", self.myskills),
("closingtext", self.closingtext),
("body", self.body),
]
def define(file, name, data, whitespace=""): file.write("\\def \\" + name + "{" + data + whitespace + "}\n")
2021-07-23 23:09:08 -04:00
def generate(data: CLData, unique):
proj_dir = os.path.dirname(os.getcwd()) + '/undercover/'
temp_dir = proj_dir + '/temp/'
unique_file = temp_dir + unique + ".tex"
2021-07-23 23:09:08 -04:00
f = open(unique_file, "w")
define(f, "username", data.username)
define(f, "thecompany", data.company)
define(f, "jobandpronoun", data.jobandpronoun, " ")
2021-07-24 01:08:18 -04:00
define(f, "skilltypes", data.skilltypes, " ")
2021-07-23 23:09:08 -04:00
define(f, "myskills", data.myskills, " ")
define(f, "closingtext", data.closingtext)
define(f, "body", data.body)
f.write(open(proj_dir + "/writing_templates/base.tex", "r").read())
f.close()
base_tex = temp_dir + unique + ".tex"
com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex
2021-07-23 23:09:08 -04:00
print("COM: '" + com + "'")
#TODO: If error, don't try to return the pdf
subprocess.run(['bash', '-c', com])
2021-07-23 23:09:08 -04:00
root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/'
return send_from_directory(download, unique + ".pdf", as_attachment=True)