-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidenup.py
executable file
·143 lines (124 loc) · 4.67 KB
/
slidenup.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
#!/usr/bin/env python3
import sys
import os
import subprocess
from termcolor import colored
import tempfile
NUPARGS = ['--nup', '3x2', '--landscape']
MERGEARGS = ['--landscape']
TEXPREAMBLE = [
r'\documentclass[12pt, a4paper]{article}',
r'\usepackage{multido}',
r'\usepackage[landscape]{geometry}',
r'\voffset = -2cm',
r'\topmargin = 0cm',
r'\headheight = 1cm',
r'\headsep = 0cm',
r'\textheight = 18cm',
r'\footskip = 0cm',
r'\usepackage{fancyhdr}',
r'\pagestyle{fancy}',
r'\renewcommand{\headrulewidth}{0pt}',
]
TEXDOCUMENT = [
r'\begin{document}',
r'\multido{}{\pagecnt}{\vphantom{x}\newpage}',
r'\end{document}'
]
def TEXLABELS(header, label, pagecnt):
ret = []
ret.append(r'\newcommand{\pagecnt}{' + str(pagecnt) + r'}')
if header:
ret.append(r'\chead{' + label + r'}')
ret.append(r'\rhead{\thepage ~/~ ' + str(pagecnt) + r'}')
ret.append(r'\cfoot{}')
else:
ret.append(r'\cfoot{' + label + r'}')
ret.append(r'\rfoot{\thepage ~/~ ' + str(pagecnt) + r'}')
return ret
if len(sys.argv) < 3:
print('Usage:', sys.argv[0], 'output-file', 'input-files')
sys.exit(1)
FNULL = open(os.devnull, 'w')
def call(args):
print(colored('Executing', 'green'), colored(args[0], 'blue'), ' '.join(args[1:]))
#res = subprocess.call(args)
res = subprocess.call(args, stdout=FNULL, stderr=FNULL)
if res != 0:
print('\t', colored('FAILED', 'yellow'))
return False
return True
def check_output(args):
print(colored('Executing', 'green'), colored(args[0], 'blue'), ' '.join(args[1:]))
try:
res = subprocess.check_output(args, stderr=FNULL)
except subprocess.CalledProcessError:
print('\t', colored('FAILED', 'yellow'))
return None
return res
tempdir = tempfile.TemporaryDirectory()
print(colored('Working in temp directory', 'blue'), tempdir.name)
# First nup individual input files
print(colored('N-up-ing individual files together', 'blue'))
infiles = sys.argv[2:]
outfiles = []
for i,infile in enumerate(infiles):
outfile = os.path.join(tempdir.name, 'nup-{}.pdf'.format(i))
if not call(['pdfjam'] + NUPARGS + [infile, '--outfile', outfile]):
print(colored('N-up failed, exiting', 'red'))
sys.exit(1)
outfiles.append(outfile)
def number_and_label(infile, outfile, label, header = True):
print(colored('Numbering and labeling', 'green'), infile)
# Use pdfinfo to count number of pages
out = check_output(['pdfinfo', infile])
if out is None:
print(colored('Calling pdfinfo failed', 'yellow'))
return False
pagecnt = None
for line in out.decode('utf-8').split('\n'):
if line.startswith('Pages:'):
pagecnt = int(line.split()[1])
break
if pagecnt is None:
print(colored('Weird pdfinfo output', 'yellow'))
return False
print(pagecnt)
# Generate tex file for numbering
texfile = os.path.join(tempdir.name, 'numbering.tex')
with open(texfile, 'w') as texf:
texf.writelines(TEXPREAMBLE)
texf.writelines(TEXLABELS(header, label, pagecnt))
texf.writelines(TEXDOCUMENT)
# Compile using pdflatex
if not call(['pdflatex', '-aux_directory='+tempdir.name, '-output-directory='+tempdir.name, texfile]):
print(colored('Running pdflatex failed', 'yellow'))
return False
numfile = os.path.join(tempdir.name, 'numbering.pdf')
# Merge with input file
if not call(['pdftk', infile, 'multistamp', numfile, 'output', outfile]):
print(colored('Combining file with labels failed', 'yellow'))
return False
return True
# Number and label each nuped file first
print(colored('Numbering the N-up-ed files individually', 'blue'))
infiles = outfiles
outfiles = []
for i,infile in enumerate(infiles):
outfile = os.path.join(tempdir.name, 'nup-numbered-{}.pdf'.format(i))
if not number_and_label(infile, outfile, os.path.basename(sys.argv[2+i]), True):
print(colored('Numbering and labeling failed, exiting', 'red'))
sys.exit(1)
outfiles.append(outfile)
# Merge numbered nuped files together
print(colored('Merging together to form single file', 'blue'))
mergefile = os.path.join(tempdir.name, 'merged.pdf')
if not call(['pdfjam'] + MERGEARGS + outfiles + ['--outfile', mergefile]):
print(colored('Merge failed, exiting', 'red'))
sys.exit(1)
# Number and label merged file
print(colored('Numbering the final merged file', 'blue'))
if not number_and_label(mergefile, sys.argv[1], '', False):
print(colored('Final numbering failed, exiting', 'red'))
sys.exit(1)
print(colored('Finished, output written to', 'green'), colored(sys.argv[1], 'blue'))