-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
123 lines (101 loc) · 3.07 KB
/
convert.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
import os
import uuid
import subprocess
from pypdf import PdfWriter
from django.conf import settings
from django.template import Template, Context
from utils.logger import get_logger
logger = get_logger(__name__)
MEMORY_LIMIT_ARG = ['+RTS', '-M256M', '-RTS']
PANDOC_CMD = ['pandoc']
LATEX = ['--pdf-engine=xelatex']
def generate_pdf_cover_sheet(cover_sheet_content, context):
template = Template(cover_sheet_content)
html_content = template.render(Context(context))
temp_dir = os.path.join(
settings.BASE_DIR,
'files',
'temp',
)
media_dir = os.path.join(
settings.BASE_DIR,
'media',
)
cover_sheet_html_path = os.path.join(
temp_dir,
f"{uuid.uuid4()}.html",
)
with open(cover_sheet_html_path, 'w') as cover_sheet_html_file:
cover_sheet_html_file.write(
html_content,
)
cover_sheet_pdf_path = os.path.join(
temp_dir,
f"{uuid.uuid4()}.pdf"
)
pandoc_command = (
PANDOC_CMD
+ MEMORY_LIMIT_ARG
+ LATEX
+ ['-s', cover_sheet_html_path, '-t', 'pdf', '-o', cover_sheet_pdf_path, f'--resource-path={media_dir}']
)
try:
logger.info("[PANDOC] Running command '{}'".format(pandoc_command))
subprocess.run(
pandoc_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
except subprocess.CalledProcessError as e:
raise PandocError("PandocError: {e.stderr}".format(e=e))
os.unlink(cover_sheet_html_path)
return cover_sheet_pdf_path
def generate_pdf_from_doc(file):
file_path = file.self_article_path()
_, extension = os.path.splitext(file_path)
if extension not in ['.docx', '.doc', '.rtf', '.odt']:
raise TypeError("File Extension {} not supported".format(extension))
out_filename = f"{uuid.uuid4()}.pdf"
out_path = os.path.join(
settings.BASE_DIR,
'files',
'articles',
str(file.article_id),
out_filename,
)
pandoc_command = (
PANDOC_CMD
+ MEMORY_LIMIT_ARG
+ LATEX
+ ['-s', file_path, '-t', 'pdf', '-o', out_path]
)
try:
logger.info("[PANDOC] Running command '{}'".format(pandoc_command))
subprocess.run(
pandoc_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
except subprocess.CalledProcessError as e:
raise PandocError("PandocError: {e.stderr}".format(e=e))
return out_path
def splice_pdf_files_together(article_file_path, cover_file_path):
merger = PdfWriter()
out_file_path = os.path.join(
settings.BASE_DIR,
'files',
'temp',
f"{uuid.uuid4()}.pdf"
)
for pdf in [cover_file_path, article_file_path]:
merger.append(pdf)
merger.write(out_file_path)
merger.close()
os.unlink(cover_file_path)
return out_file_path
class PandocError(Exception):
def __init__(self, msg, cmd=None):
super().__init__(self, msg)
self.cmd = cmd