-
Notifications
You must be signed in to change notification settings - Fork 8
/
pdf_bookmark.py
executable file
·662 lines (498 loc) · 16.9 KB
/
pdf_bookmark.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#!/usr/bin/env python
# pylint: disable=invalid-name
'''
Import and export PDF bookmark
'''
import os
import sys
import subprocess
import re
import argparse
import json
import tempfile
import codecs
VERSION = '1.1.0'
_NUM_STYLE_MAP = {
'DecimalArabicNumerals': 'Arabic',
'UppercaseRomanNumerals': 'Roman',
'LowercaseRomanNumerals': 'Roman',
'UppercaseLetters': 'Letters',
'LowercaseLetters': 'Letters',
}
_ROMAN_NUMERAL_PAIR = (
('M', 1000),
('CM', 900),
('D', 500),
('CD', 400),
('C', 100),
('XC', 90),
('L', 50),
('XL', 40),
('X', 10),
('IX', 9),
('V', 5),
('IV', 4),
('I', 1),
)
_ROMAN_NUMERAL_MAP = {pair[0]: pair[1] for pair in _ROMAN_NUMERAL_PAIR}
_ROMAN_NUMERAL_PATTERN = re.compile(
'^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
)
_BOOKMARK_DESCRIPTION = {
'bookmark': {
'prefix': 'Bookmark',
'fields': {
'Title': 'title',
'Level': 'level',
'PageNumber': 'page',
},
'handler': {
'title': lambda s: _unicode_replace(s) if _UNICODE_REGEXP.search(s) else s,
'level': int,
'page': int,
},
},
'page_label': {
'prefix': 'PageLabel',
'fields': {
'NewIndex': 'new_index',
'Start': 'num_start',
'NumStyle': 'num_style',
},
'handler': {
'new_index': int,
'num_start': int,
'num_style': lambda s: _NUM_STYLE_MAP.get(s, 'Arabic'),
},
},
}
_UNICODE_REGEXP = re.compile('&#([0-9]+);')
_CONTENT_MINIMUM_DOTS = 4
class CommandError(Exception):
'''Run command error'''
class InvalidBookmarkSyntaxError(Exception):
'''Invalid bookmark syntax'''
class InvalidNumeralError(ValueError):
'''Invalid numeral expression'''
class InvalidRomanNumeralError(InvalidNumeralError):
'''Invalid roman numeral expression'''
class RomanOutOfRangeError(Exception):
'''The roman number is out of range'''
class InvalidLettersNumeralError(InvalidNumeralError):
'''Invalid letters numeral expression'''
class LettersOutOfRangeError(Exception):
'''The letters number is out of range'''
class PdfMarkError(Exception):
'''Error dealing with pdfmark'''
def echo(s, nl=True, err=False):
'''
Print to stdout
'''
if err:
out = sys.stderr
else:
out = sys.stdout
out.write(s)
if nl:
out.write('\n')
out.flush()
def roman_to_arabic(roman):
'''
Convert roman to arabic
'''
if not roman:
raise InvalidRomanNumeralError('No input found')
if roman == 'N':
return 0
if not _ROMAN_NUMERAL_PATTERN.match(roman):
raise InvalidRomanNumeralError(
'Invalid Roman numeral: {}'.format(roman))
arabic = 0
for i, n in enumerate(roman):
if i == len(roman)-1 or _ROMAN_NUMERAL_MAP[roman[i]] >= _ROMAN_NUMERAL_MAP[roman[i+1]]:
arabic += _ROMAN_NUMERAL_MAP[n]
else:
arabic -= _ROMAN_NUMERAL_MAP[n]
return arabic
def arabic_to_roman(arabic):
'''
Convert arabic to roman
'''
if arabic < 0 or arabic > 4999:
raise RomanOutOfRangeError('Roman numeral must in [0, 5000)')
if arabic == 0:
return 'N'
roman = ''
remain = arabic
for digit, unit in _ROMAN_NUMERAL_PAIR:
digit_num = remain // unit
roman += digit*digit_num
remain -= unit*digit_num
return roman
def letters_to_arabic(letters):
'''
Convert letters to arabic
'''
if not letters:
return 0
letter = letters[0]
if ord(letter) < ord('A') or ord(letter) > ord('Z'):
raise InvalidLettersNumeralError('Must be capital letter')
for digit in letters[1:]:
if digit != letter:
raise InvalidLettersNumeralError('Letters are not identical')
return len(letters)*26 - 25 + ord(letter) - ord('A')
def arabic_to_letters(arabic):
'''
Convert arabic to letters
'''
if arabic < 0:
raise LettersOutOfRangeError('Letters numeral must >= 0')
if arabic == 0:
return ''
return chr(((arabic-1) % 26) + ord('A')) * ((arabic+25) // 26)
def _unicode_replace_match(match):
return chr(int(match.group(1)))
def _unicode_replace(string):
return _UNICODE_REGEXP.sub(_unicode_replace_match, string)
def call(cmd, encoding=None):
'''
Run command
'''
if encoding is None:
encoding = 'utf-8'
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except FileNotFoundError:
raise CommandError('Command not installed: {}'.format(cmd[0]))
out, err = p.communicate()
status = p.wait()
if status != 0:
raise CommandError(
'Invoke command {} failed with exit code {}:\n {}'.format(
cmd, status, err.decode(encoding)))
if encoding:
out = out.decode(encoding)
return out
def import_pdftk(data, collapse_level=0):
'''
Convert pdftk output to bookmark
'''
bookmarks = {}
bookmark_info = {}
for t in _BOOKMARK_DESCRIPTION:
bookmarks[t] = []
bookmark_info[t] = {}
for line in data.splitlines():
try:
key, value = line.split(': ', 1)
except ValueError: # e.g. line == 'InfoBegin'
continue
for bm_type, bm_detail in _BOOKMARK_DESCRIPTION.items():
if not key.startswith(bm_detail['prefix']):
continue
k = key[len(bm_detail['prefix']):]
if k not in bm_detail['fields']:
continue
k = bm_detail['fields'][k]
if k in bm_detail['handler']:
v = bm_detail['handler'][k](value)
bookmark_info[bm_type][k] = v
ready_for_save = True
for _, field in bm_detail['fields'].items():
if field not in bookmark_info[bm_type]:
ready_for_save = False
break
if not ready_for_save:
continue
bookmark_info[bm_type]['collapse'] = collapse_level != 0 and \
bookmark_info[bm_type]['level'] >= collapse_level
bookmarks[bm_type].append(bookmark_info[bm_type])
bookmark_info[bm_type] = {}
return bookmarks
def export_bmk(bookmarks):
'''
Export to bookmark format
'''
bm_output = '!!! # Generated bmk file\n'
page_labels = bookmarks['page_label']
current_page_label_index = -1
current_collapse_level = 0
for bm in bookmarks['bookmark']:
page_label_index = -1
for i, pl in enumerate(page_labels):
if bm['page'] >= pl['new_index']:
page_label_index = i
if page_label_index >= 0:
if page_label_index != current_page_label_index:
bm_output += '\n'
for k in ['new_index', 'num_start', 'num_style']:
bm_output += '!!! {} = {}\n'.format(
k, page_labels[page_label_index][k])
bm_output += '\n'
current_page_label_index = page_label_index
page = bm['page'] - \
page_labels[page_label_index]['new_index'] + \
page_labels[page_label_index]['num_start']
if page_labels[page_label_index]['num_style'] == 'Roman':
page = arabic_to_roman(page)
elif page_labels[page_label_index]['num_style'] == 'Letters':
page = arabic_to_letters(page)
else:
page = bm['page']
# This is a XOR of (bm['collapse']) and
# (current_collapse_level == 0 or current_collapse_level > bm['level'])
if bm['collapse'] == (current_collapse_level == 0 or current_collapse_level > bm['level']):
current_collapse_level = bm['level'] if bm['collapse'] else 0
bm_output += '!!! collapse_level = {}\n'.format(
current_collapse_level)
bm_output += '{}{}................{}\n'.format(
' '*(bm['level']-1), bm['title'], page)
return bm_output
def _parse_bookmark_command(line):
if line[3:].lstrip().startswith('#'):
return '', ''
try:
k, v = line[3:].split('=', 1)
except ValueError:
raise InvalidBookmarkSyntaxError('Invalid syntax: {}'.format(line))
return k.strip(), v.strip()
def _parse_level(line, level_indent):
space_count = 0
for c in line:
if c != ' ':
break
space_count += 1
if space_count % level_indent != 0:
raise InvalidBookmarkSyntaxError(
'Level indentation error: {}'.format(line))
return space_count // level_indent + 1, line[space_count:]
def _split_title_page(title_page):
start_pos = title_page.find('.'*_CONTENT_MINIMUM_DOTS)
if start_pos < 0:
raise InvalidBookmarkSyntaxError(
'There must be at least {} "." specified in the line "{}"'.format(_CONTENT_MINIMUM_DOTS,title_page))
end_pos = start_pos + _CONTENT_MINIMUM_DOTS
for c in title_page[start_pos+_CONTENT_MINIMUM_DOTS:]:
if c != '.':
break
end_pos += 1
title = title_page[:start_pos]
page = title_page[end_pos:]
return title.strip(), page.strip()
def import_bmk(bookmark_data, collapse_level=0):
'''
Import bookmark format
'''
bookmarks = {}
bookmarks['bookmark'] = []
bookmarks['page_label'] = []
page_config = {
'new_index': 1,
'num_start': 1,
'num_style': 'Arabic',
'collapse_level': collapse_level,
'level_indent': 2,
}
page_label_saved = False
for line in bookmark_data.splitlines():
if not line.strip():
continue
if line.startswith('!!!'):
k, v = _parse_bookmark_command(line)
if not k:
continue
if k == 'new_index':
page_label_saved = False
page_config[k] = int(v)
page_config['num_start'] = 1
page_config['num_style'] = 'Arabic'
elif k in ['num_start', 'collapse_level', 'level_indent']:
page_config[k] = int(v)
else:
page_config[k] = v
continue
if not page_label_saved:
bookmarks['page_label'].append({kk: vv for kk, vv in page_config.items() if kk in [
'new_index', 'num_start', 'num_style']})
page_label_saved = True
level, title_page = _parse_level(line, page_config['level_indent'])
title, page = _split_title_page(title_page)
try:
if page_config['num_style'] == 'Roman':
page = roman_to_arabic(page.upper())
elif page_config['num_style'] == 'Letters':
page = letters_to_arabic(page.upper())
else:
page = int(page)
except ValueError:
raise InvalidBookmarkSyntaxError(
'Page number invalid: {}'.format(page))
page = page - page_config['num_start'] + page_config['new_index']
collapse = page_config['collapse_level'] != 0 and level >= page_config['collapse_level']
bookmark_info = {
'level': level,
'title': title,
'page': page,
'collapse': collapse,
}
bookmarks['bookmark'].append(bookmark_info)
return bookmarks
def _pdfmark_unicode(string):
r"""
>>> _pdfmark_unicode('ascii text with ) paren')
'(ascii text with \\) paren)'
>>> _pdfmark_unicode('\u03b1\u03b2\u03b3')
'<FEFF03B103B203B3>'
"""
try:
string.encode('ascii')
except UnicodeEncodeError:
b = codecs.BOM_UTF16_BE + string.encode('utf-16-be')
return '<{}>'.format(''.join('{:02X}'.format(byte) for byte in b))
else:
# escape special characters
for a, b in [('\\', '\\\\'), ('(', '\\('), (')', '\\)'),
('\n', '\\n'), ('\t', '\\t')]:
string = string.replace(a, b)
return '({})'.format(string)
def _pdfmark_unicode_decode(string):
r"""
>>> _pdfmark_unicode_decode(_pdfmark_unicode('\u03b1\u03b2\u03b3'))
'\u03b1\u03b2\u03b3'
"""
if not (string.startswith('<FEFF') and string.endswith('>')):
raise PdfMarkError
b = bytes(int(float.fromhex(x1+x2))
for x1, x2 in zip(string[5:-2:2], string[6:-1:2]))
return b.decode('utf-16-be')
def export_pdfmark(bookmarks):
'''
Convert bookmark to pdfmark
'''
pdfmark = ''
for i, bm in enumerate(bookmarks['bookmark']):
pdfmark += '['
count = 0
for bmk in bookmarks['bookmark'][i+1:]:
if bmk['level'] == bm['level']:
break
if bmk['level'] == bm['level'] + 1:
count += 1
if count:
sign = '-' if bm.get('collapse') else ''
pdfmark += '/Count {}{} '.format(sign, count)
pdfmark += '/Title {} /Page {} '.format(
_pdfmark_unicode(bm['title']), bm['page'])
pdfmark += '/OUT pdfmark\n'
return pdfmark
def _write_pdfmark_noop_file():
# By default, Ghostscript will preserve pdfmarks from the sources PDFs
fd, filename = tempfile.mkstemp(prefix='pdfmark-noop-', text=True)
# Make `[... /OUT pdfmark` a no-op.
os.write(fd, b"""
% store the original pdfmark
/originalpdfmark { //pdfmark } bind def
% replace pdfmark with a wrapper that ignores OUT
/pdfmark
{
{ % begin loop
{ counttomark pop }
stopped
{ /pdfmark errordict /unmatchedmark get exec stop }
if
dup type /nametype ne
{ /pdfmark errordict /typecheck get exec stop }
if
dup /OUT eq
{ (Skipping OUT pdfmark\n) print cleartomark exit }
if
originalpdfmark exit
} loop
} def
""")
os.close(fd)
return filename
def _write_pdfmark_restore_file():
fd, filename = tempfile.mkstemp(prefix='pdfmark-restore-', text=True)
# Restore the default `[... /Out pdfmark` behaviour
os.write(fd, b'/pdfmark { originalpdfmark } bind def\n')
os.close(fd)
return filename
def _write_pdfmark_pagemode():
fd, filename = tempfile.mkstemp(prefix='pagemode_', text=True)
os.write(
fd, b'[/PageMode /UseOutlines /View [/FitH] /Page 1 /DOCVIEW pdfmark\n')
os.close(fd)
return filename
def generate_pdf(pdfmark, pdf, output_pdf):
'''
Generate pdf from pdfmark and pdf file
'''
fd, pdfmark_file = tempfile.mkstemp(prefix='pdfmark_', text=True)
os.write(fd, pdfmark.encode('ascii'))
os.close(fd)
pdfmark_noop = _write_pdfmark_noop_file()
pdfmark_restore = _write_pdfmark_restore_file()
pdfmark_pagemode = _write_pdfmark_pagemode()
call(['gs', '-dBATCH', '-dNOPAUSE', '-sDEVICE=pdfwrite', '-dAutoRotatePages=/None',
'-sOutputFile={}'.format(output_pdf),
pdfmark_noop,
pdf,
pdfmark_restore,
pdfmark_file,
pdfmark_pagemode])
os.remove(pdfmark_noop)
os.remove(pdfmark_restore)
os.remove(pdfmark_file)
os.remove(pdfmark_pagemode)
def main():
'''
The main process
'''
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-f', '--format', default='bmk',
choices=['bmk', 'none', 'pdftk', 'pdfmark', 'json'],
help='the output format of bookmark')
parser.add_argument(
'-l', '--collapse-level', default=0, type=int,
help='the min level to be collapsed, 0 to expand all')
parser.add_argument(
'-b', '--bookmark', help='the bookmark file to be imported')
parser.add_argument(
'-p', '--pdf', help='the input PDF file')
parser.add_argument(
'-o', '--output-pdf', help='the output PDF file')
parser.add_argument(
'-v', '--version', action='store_true', help='version of pdf-bookmark')
args = parser.parse_args()
if args.version:
echo('pdf-bookmark version {}'.format(VERSION))
return 0
if args.bookmark is None and args.pdf is None or \
args.pdf is None and args.output_pdf is not None:
parser.print_help(sys.stderr)
return 1
if args.bookmark is not None:
with open(args.bookmark) as f:
bookmarks = import_bmk(f.read(), args.collapse_level)
else:
pdftk_data = call(['pdftk', args.pdf, 'dump_data'], 'ascii')
if args.format == 'pdftk':
echo(pdftk_data, nl=False)
bookmarks = import_pdftk(pdftk_data, args.collapse_level)
if args.format == 'pdfmark' or (args.output_pdf is not None and args.pdf is not None):
pdfmark = export_pdfmark(bookmarks)
if args.format == 'json':
echo(json.dumps(bookmarks))
elif args.format == 'bmk':
echo(export_bmk(bookmarks), nl=False)
elif args.format == 'pdfmark':
echo(pdfmark, nl=False)
if args.output_pdf is not None:
generate_pdf(pdfmark, args.pdf, args.output_pdf)
return 0
if __name__ == '__main__':
sys.exit(main())