2012-01-30

Generating PDF Bookmarks Automatically When Combining PDFs

A quick script to generate a proper bookmark file for jpdfbookmarks from a list of PDFs.  I find myself needing this enough, the "evince on one monitor, vim on the other" method became tedious.

autobookmark.sh:


#!/bin/sh
p=1;
for f in *.pdf
do
   fn=`printf "$f" | sed 's/\.pdf$//i'`
   printf "%02d - %s/%d,FitPage\n" $p "$fn" $p
   p=$(($p+`pdfinfo "$f" | awk '/Pages/ {print $2}'`))
done

So, given this input:
Chipsets_And_Datasheets/Initio_Corporation $ ls -1
Initio-2009_Bridge_Controller_Roadmap.pdf
Initio-INIC1430-P1394_to_ATA_Bridge_Spec.pdf
Initio-INIC1511-USB_to_PATA_Bridge_Spec.pdf
Initio-INIC1606-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1608-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1610-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1611-USB_to_SATA_Bridge_Spec.pdf
Initio-INIC1622-PCI_to_SATA.pdf
Initio-INIC2430-Firewire_to_ATA_Bridge_IC.pdf


The output would be (with some slight cleanup specific to this input):
$ autobookmark.sh | sed 's/Initio-//;s/_/ /g;s/-/ - /2' > bookmarks.txt
$ cat bookmarks.txt
01 - 2009 Bridge Controller Roadmap/1,FitPage
10 - INIC1430 - P1394 to ATA Bridge Spec/10,FitPage
47 - INIC1511 - USB to PATA Bridge Spec/47,FitPage
73 - INIC1606 - USB to SATA Bridge Spec/73,FitPage
88 - INIC1608 - USB to SATA Bridge Spec/88,FitPage
123 - INIC1610 - USB to SATA Bridge Spec/123,FitPage
159 - INIC1611 - USB to SATA Bridge Spec/159,FitPage
174 - INIC1622 - PCI to SATA/174,FitPage
257 - INIC2430 - Firewire to ATA Bridge IC/257,FitPage


And then it's a simple matter to make the pdf compilation and apply:
$ pdftk *.pdf cat output Initio_Chipset_Datasheets.pdf
$ jpdfbookmarks Initio_Chipset_Datasheets.pdf --apply bookmarks.txt --force

Requirements:
pdftk - The PDF Toolkit
JPdfBookmarks - Excellent util for adding bookmarks to PDFs.
pdfinfo - XPDF viewer package.

No comments: