Editing PDFs is always a headache. I don’t most of the time, but I was always scrambling to find a GUI tool when I need to. As I moved from Windows to Linux, my options became even more limited. For a long time, I was using free online PDF editors. It was enough, until I wanted to edit a PDF with sensitive private information. That forced me to explore command line tools. I had found a few since then. Here is a little cheatsheet to share.

Image to pdf

Installation

pip3 install img2pdf
# or
sudo apt install img2pdf

Example

img2pdf --pagesize A4 -o enbridge_bill.pdf IMG_20220201_110317~2.jpg IMG_20220201_110347~2.jpg IMG_20220201_110454~2.jpg IMG_20220201_110522~2.jpg

pagesize options: support A<num> sizing and Letter it’s case sensitive

pdftk

Installation

sudo apt install pdftk

Change pdf bookmarks

You can create a new pdf file with the new metadata, e.g. bookmark. This is particularly useful for me because Okular’s bookmark system stores the user added bookmark somewhere else entirely.

Get existing pdf metadata

pdftk <pdf_path> dump_data

# could save stdout to file
pdftk target-pdf.pdf dump_data >> ./metadata

update the metadata file

Code snippet for bookmarks

NumberOfPages: 161
# what goes before
BookmarkBegin
BookmarkTitle: <title>
BookmarkLevel: 2
BookmarkPageNumber: <page>

# what goes after
PageMediaBegin

Create new pdf with new metadata

pdftk <original_pdf> update_info <new_metadata> output <new_pdf>

Merge PDFs

pdftk A=<file path> B=<file path> ... cat <list of files aliases> output <output file path>
pdftk A=file1.pdf B=file2.pdf cat A B output out.pdf

Honorable Mention

pdftools (python) . It support merge, add, insert, zip, …