"""Merge PDFs perserving bookmarks.
-Thanks to Larry Cai for suggesting that Unicode be supported.
+Thanks to Larry Cai for suggesting that Unicode be supported and for
+discussion about the `--pdfmarks` option.
"""
import codecs as _codecs
import locale as _locale
import os as _os
+import os.path as _os_path
import re as _re
import subprocess as _subprocess
import sys as _sys
help='path to the pdftk executable')
parser.add_argument('--gs', dest='gs', default=GS,
help='path to the gs (Ghostscript) executable')
+ parser.add_argument('--pdfmarks', dest='pdfmarks',
+ help=('path to pdfmarks file. If not given, a '
+ 'temporary file is used. If given and the file '
+ 'is missing, execution will stop after the file '
+ 'is created (before the Ghostscript run). If '
+ 'given and the file exists, no attempt will be '
+ 'make to use pdftk to generate the mark file (I '
+ 'assume your input file is what you want).'))
parser.add_argument('--argv-encoding', dest='argv_encoding',
help=('Optionally override the locale encoding for '
'your command line arguments.'))
keywords = [unicode(k, argv_encoding) for k in args.keywords]
else:
keywords = None
- pdfmarks = generate_pdfmarks(
- inputs, title=title, author=author, keywords=keywords)
+ if args.pdfmarks and _os_path.isfile(args.pdfmarks):
+ pdfmarks = open(args.pdfmarks, 'r').read()
+ else:
+ pdfmarks = generate_pdfmarks(
+ inputs, title=title, author=author, keywords=keywords)
+ if args.pdfmarks:
+ open(args.pdfmarks, 'w').write(pdfmarks)
+ _sys.exit(0)
merge_pdfs(inputs=inputs, pdfmarks=pdfmarks, output=args.output,
pause_for_manual_tweaking=args.pause_for_manual_tweaking)