-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfoutline.py
159 lines (122 loc) · 4.94 KB
/
pdfoutline.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import re
import subprocess
import tempfile, os, sys
import argparse
class Entry():
def __init__(self, name, page, children):
self.name = name
self.page = page
self.children = children # Entry list
def pritty_print(self, depth):
print(depth * ' ' + self.name + ':' + str(self.page))
for c in self.children:
c.pritty_print(depth+1)
# Parse the start of the line for whitespace characters and return "tab";
# should only be called once on first occurrence of an indent while tab == ""
def parse_tab(line):
tab = ""
# add whitespace characters to tab
for ch in line:
if (ch.isspace()):
tab += ch
else:
break
return tab
def toc_to_elist(toc):
tab = "" # indentation character(s) evaluated and assigned to this later
lines = toc.split('\n')
cur_entry = [[]] # current entries by depth
offset = 0
for line in lines:
# if indentation style hasn't been evaluated yet and the line starts
# with a whitespace character, assume its an indent and assign all the
# leading whitespace to tab
if ((tab == "") and (line != "") and (line[0].isspace())):
tab = parse_tab(line)
depth = 0
# determine depth level of indent
if (tab != ""):
# find length of leading whitespace in string
ws_len = 0
for ch in line:
if (ch.isspace()):
ws_len += 1
else:
break
# count indent level up to first non-whitespace character;
# allows for "indents" to appear inside section titles e.g. if an
# indent level of a single space was chosen
depth = line.count(tab, 0, ws_len)
line = line.split('#')[0].strip() # strip comments and white spaces
if not line:
continue
# if line[0] == '+':
# offset += int(line[1:])
# continue
# if line[0] == '-':
# offset -= int(line[1:])
# continue
try:
(name, page) = re.findall(r'(.*) (\d+)$', line)[0]
page = int(page) + offset
cur_entry = cur_entry[:depth+1] + [[]]
cur_entry[depth].append(Entry(name, page, cur_entry[depth+1]))
except:
# todo display line number
print('syntax error in toc-file. line:\n' + line)
exit(1)
return cur_entry[0]
def elist_to_gs(elist):
def rec_elist_to_gslist(elist):
gs_list = []
for entry in elist:
gs_list.append("[/Page %d /View [/XYZ null null null] /Title <%s> /Count %d /OUT pdfmark" \
% (entry.page, "FEFF" + entry.name.encode("utf-16-be").hex(), len(entry.children)))
gs_list += rec_elist_to_gslist(entry.children)
return gs_list
return '\n'.join(rec_elist_to_gslist(elist))
def pdfoutline(inpdf, tocfilename, outpdf, gs='gs'):
with open(tocfilename) as f:
toc = f.read()
gs_command = elist_to_gs(toc_to_elist(toc))
tmp = tempfile.NamedTemporaryFile(mode = 'w', delete=False)
tmp.write(gs_command)
tmp.close()
process = subprocess.Popen(\
[gs, '-o', outpdf, '-sDEVICE=pdfwrite', tmp.name, '-f', inpdf],\
stdout=subprocess.PIPE)
# show progress bar
totalPage = 0
for line in process.stdout:
tot = re.findall(r'Processing pages 1 through (\d+)', line.decode('ascii'))
if tot:
totalPage = int(tot[0])
printProgressBar(0, totalPage)
break
for line in process.stdout:
currentPage = re.findall(r'Page (\d+)', line.decode('ascii').strip())
if currentPage:
printProgressBar(int(currentPage[0]), totalPage)
os.unlink(tmp.name)
# https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 50, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
# print('\r%s |%s| %d/%d %s' % (prefix, bar, iteration, total, suffix), end = printEnd)
# Print New Line on Complete
# if iteration == total:
# print()