Question: The PDF viewer Okular (part of KDE desktop) has lots of nice features and supports lots of file formats.
One of the things I like about it is the comments and annotations.
Unfortunately, all of the comments and metadata (like last viewed page) get stored locally in:
**~/.kde/share/apps/okular/docdata**

Is there any way to make Okular store them in the PDF itself?

Answer: 
This information is documented in [the okular documentation](http://docs.kde.org/stable/en/kdegraphics/okular/annotations.html).

As alxs mentioned, a method supported for a while now is to save the PDF document as an archive, which creates a `.okular` file. Saving the annotations directly in the PDF is quite recent, and from the documentation this option is only available from version 0.15. With such a recent version, using `Save As` and saving as a PDF stores the annotations as well. Note as well that, as mentioned in the documentation, this also requires a recent version of `poppler`.

A problem you might have, however, is that a document saved as an archive cannot be currently saved back as a pdf. Therefore, if you already made your annotations, and want to use a newer version of `okular` to save these in the PDF, it may get tricky. If you just update your version of okular, this may work, since it will use the same `~/.kde/share/apps/okular/docdata` folder. However if you want to use a newer version of okular on another system, you should transfer the PDF and the corresponding metadata file separately.
Current version of Okular allows one to save the PDF with the annotations by going into File -> Save As.

However, I wanted something automated. So, I created an Autokey script so that whenever I close my PDF, the annotations are automatically saved in the PDF itself. Note that **this script will save your PDF overwriting the original PDF.**

The Autokey Script
------------------

First, you will need to install `autokey-gtk` and `xdotool` first:

```
sudo apt-get install autokey-gtk xdotool

```

Now, in autokey, go to New -> Script. Add the following code to your new script:

```python
#This is used to save PDF in okular so that the annotations persist in the PDF file itself
#We have to use to `xdotool` to bring the dialogs back into focus, otherwise they are losing focus 
import subprocess

keyboard.send_keys("<ctrl>+<shift>+s")
time.sleep(0.4)
subprocess.call(["xdotool", "windowfocus", "`xdotool getwindowfocus`"])
time.sleep(0.1)
keyboard.send_key("<enter>")
time.sleep(0.1)
subprocess.call(["xdotool", "windowfocus", "`xdotool getwindowfocus`"])
time.sleep(0.1)
keyboard.send_key("<tab>")
time.sleep(0.1)
keyboard.send_key("<enter>")
time.sleep(0.1)
subprocess.call(["xdotool", "windowfocus", "`xdotool getwindowfocus`"])
time.sleep(0.5)
keyboard.send_keys("<ctrl>+q") #Quit Finally

```

You can now assign a window filter and hotkey to this script. In window filter, add `.*okular.*`. And in hotkey, I've used `<ctrl>+s`. You could use anything else that you prefer.

So, now whenever I have to quit okular, I use `Ctrl``S`, and okular quits after saving my pdf.