Table of contents
Open Table of contents
Introduction
This piece of text introduces a command line tool for PDF manipulation purposes, it is named cpdf
. This tool is useful, because although most free PDF views have basic PDF viewing functionalities, PDF editing functionalities such as merging/splitting, adding bookmarks, encrypting/decrypting are usually behind the paywall. cpdf
is a convenient tool that can perform all the functionalities described above, and more. I will walk through a simulated usage of cpdf
now.
Installation
I have a class that posts encrypted notes of each lecture as PDF on a website, each lecture PDF doesn’t contain any bookmarks. Ultimately, I want to create an unencrypted PDF that contains all notes from that class, and the PDF should contain a table of contents so that I can skip to any part of the text at ease.
The first step is to download all notes from the internet. Then, download cpdf
: https://community.coherentpdf.com/. I chose to download the prebuilt binary from Mac OS.
Technically we could move to the folder containing that binary and call cpdf
from there, and the program would be working correctly. However, in order to call globally, place the binary into desired location, and do
echo 'export PATH="desired-path:$PATH"' >> ~/.zshrc && source ~/.zshrc
to add folder to path. Here, I saved to .zshrc
, because the shell that I’m using is zsh. Now we can call cpdf
globally. This process should be similar for Windows and Linux, too.
Decryption
To decrypt the file, do:
cpdf in.pdf owner=password -o out.pdf
So, we could either decrypt the files one by one, or write a bash script and run a for loop to decrypt everything at once.
Merge
It is a good idea to place all decrypted files and those files alone into a separate folder. This is because then we could use this command:
cpdf -merge -idir desired-folder -o out.pdf
Else, the command will be more tedious to write:
cpdf -merge in1.pdf in2.pdf in3.pdf -o out.pdf
Of course, add as many PDFs as you like.
bookmarks
To generate the table of contents, or bookmarks, it is wise to create a new .txt
file, its content is formatted table of contents of the PDF. For me, I did something like this:
0 "Chapter 1" 1
1 "Section 1.1" 2
1 "Section 1.2" 26
0 "Chapter 2" 50
1 "Section 2.1" 51
1 "Section 2.2" 76
0 "Chapter 3" 100
1 "Section 3.1" 101
1 "Section 3.2" 126
The leading number means the level of this bookmark. The string is the name of that bookmark. The last number is the page number of the bookmark. Finally, to add this bookmark tree to the target PDF, do:
cpdf -add-bookmarks toc.txt in.pdf -o out.pdf
Combining Commands
Multiple lines of commands could be combined together using the keyword AND
. In my case, I could’ve done something like this:
cpdf in1.pdf owner=password -o out1.pdf AND
cpdf in2.pdf owner=password -o out2.pdf AND
cpdf in3.pdf owner=password -o out3.pdf AND
cpdf -merge -idir desired-folder -o out.pdf AND
cpdf -add-bookmarks toc.txt out.pdf -o out.pdf
Even better, save all those commands in another file control.txt
, and use
cpdf -args control.txt
And technically everything should carry out by themselves automatically. However, when I tried to use the above command, sometimes errors occur. But even then we could simply copy paste the text from control.txt
and run in terminal.
Conclusion
That’s pretty much it. For more usages, consult the official documentation. Just wanted to share this nice tool.