mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / PDF_bookmarks_with_Ghostscript.mdwn
1 I was recently trying to add bookmarks to a [[PDF]] I'd generated with
2 [[pdftk]].  It turns out to be fairly simple to add bookmarks to a PDF
3 using [Ghostscript][], following [maggoteer's post][post] to the
4 Ubuntu forums.  The syntax is:
5
6     $ gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf in-*.pdf pdfmarks
7
8 Where `out.pdf` is the generated PDF, `in-*.pdf` are the input PDFs,
9 and `pdfmarks` is a text file with contents like:
10
11     [/Title (Title Page) /Page 1 /OUT pdfmark
12     [/Title (Table of Contents) /Page 3 /OUT pdfmark
13     ...
14
15 Nice and easy.
16
17 For nested levels, use the `/Count` attribute.  For example:
18
19     [/Count 3 /Title (Chapter 1) /Page 1 /OUT pdfmark
20     [/Count -2 /Title (Section 1.1) /Page 2 /OUT pdfmark
21     [/Title (Section 1.1.1) /Page 3 /OUT pdfmark
22     [/Title (Section 1.1.2) /Page 4 /OUT pdfmark
23     [/Count -1 /Title (Section 1.2) /Page 5 /OUT pdfmark
24     [/Title (Section 1.2.1) /Page 6 /OUT pdfmark
25     [/Title (Section 1.3) /Page 7 /OUT pdfmark
26
27 The argument to `/Count` gives the number of immediately subordinate
28 bookmarks.  The sign of the argument sets the default display
29 (negative for closed, positive for open).
30
31 You can also setup the document info dictionary with something like:
32
33     [ /Title (My Test Document)
34       /Author (John Doe)
35       /Subject (pdfmark 3.0)
36       /Keywords (pdfmark, example, test)
37       /DOCINFO pdfmark
38
39 If you want more detail, take a look at [Adobe's pdfmark
40 reference][reference].
41
42 I've bundled the whole pdfmarks-generation bit into a script,
43 [[pdf-merge.py]], which generates the pdfmark file and runs
44 Ghostscript automatically.  Think of it as a bookmark-preserving
45 version of pdftk's `cat`.  The script uses [[pdftk]] internally to
46 extract bookmark information from the source PDFs.
47
48 The script also adds a bit of PostScript to ignore any bookmarks in
49 the source PDFs during the Ghostscript run.  The only bookmarks in the
50 output will be the ones you specify explicitly in the pdfmarks file.
51 If for some reason the automatically generated pdfmarks are not quite
52 what you want, the script can pause (via `--ask`) to allow you to
53 tweak the pdfmarks manually before running Ghostscript.
54
55 [Ghostscript]: http://ghostscript.com/
56 [post]: http://ubuntuforums.org/showthread.php?t=1545064
57 [reference]: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdfmark_reference.pdf
58
59 [[!tag tags/tools]]
60 [[!tag tags/linux]]