-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
33 lines (26 loc) · 1.02 KB
/
utils.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
from weasyprint import HTML,CSS
from uuid import uuid4
import os
import subprocess
ALLOWED_EXTENSIONS = set(['doc', 'docx', 'pdf', 'html'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def convert_html_to_pdf(file_path, file_name):
try:
random_string = str(uuid4())[:10]
HTML(file_path).write_pdf(os.path.join('static/converted_files',
file_name+'_'+random_string+'.pdf'),
stylesheets=[CSS(string='body { font-family: serif !important;margin-left:-80px; }')])
except Exception as e:
print("Exception Occurred", e)
return False
return True
def convert_doc_to_pdf(file_path):
try:
subprocess.call(['soffice', '--headless', '--convert-to', 'pdf',
"--outdir", 'static/converted_files', file_path])
except Exception as e:
print("Exception Occurred", e)
return False
return True