Working with pdf from the context menu of the file manager

The wonderful file manager Dolphin is good for everyone. And two panels, and a variety of context menus that speed up the processing of various commands and tasks. But what if you need to quickly and easily assemble several one-page pdf files? Because of this, do not install heavy software, which is subsequently used extremely rarely. Of course not. Let’s just expand the capabilities of our manager by adding a couple of items to the context menu. Moreover, it is not at all difficult to do this. But first you need to write a couple of scripts for this very processing of pdf files. And so, let’s get started.

Working with pdf files

To process files, we will use the pdftk console program. Since the script will be embedded in the context menu, we will use the zenity utility to display information about errors, progress of execution. We will also need pop-up messages, notify-send will be responsible for them.

pdfmark – merge pdf files and add bookmarks

Since some arguments are passed to the script, then you need to check what and how much was passed via the command line. And since there are not so many arguments, we write the check in two lines. First, we check the help call using the -h and –help keys. The second line will check only the number of transferred files, it makes no sense to process one file.

Next, we create a temporary directory and already in it we create a bookmark file for the future combined pdf file. Let’s add the title of the document by the name of the directory from which we combine the file. We use the username as the name of the author of the document. Add bookmarks by file names to the bookmark file. We merge, add bookmarks, check the success of the operation by the presence of the final file and display the appropriate messages. Delete the temporary directory before exiting.

pdfmark: bookmarks and merge
    echo "InfoBegin"
    echo "InfoKey: Title"
    echo "InfoValue: ${WorkDir}"
    echo "InfoBegin"
    echo "InfoKey: Author"
    echo "InfoValue: ${USER^}"
    echo "NumberOfPages: ${#FileList[@]}"
    for BookMark in "${MarkList[@]}"; do
        echo "BookmarkBegin"
        echo "BookmarkTitle: ${BookMark}"
        echo "BookmarkLevel: 1"
        echo "BookmarkPageNumber: ${NumberPage}"
        ((NumberPage += 1))
} >>"${TmpDir}"/

# объединить выбранные документы и добавить закладки
(pdftk "${@}" cat output "${TmpDir}"/"${WorkDir}".pdf && pdftk "${TmpDir}"/"${WorkDir}".pdf update_info "${TmpDir}"/ output "${WorkDir}.pdf") | zenity --progress --title="${msg_title}" --text="${msg_progress}" --pulsate --width=500 --no-cancel --auto-close --auto-kill

pdf_service_menu – menu for processing pdf files

The second script will do more operations on pdf files. List of available operations:

  • burst – allows you to parse the entire document into pages;

  • extract – extracts the specified pages from the document;

  • remove – removes a range of pages from a document;

  • cat – combines documents;

  • add – adds the selected documents, in the order they were selected;

  • stamp – puts a stamp on the document pages;

  • pdf2jpg – allows you to convert document pages to jpg;

  • info – displays information about the document;

Here we use kdialog as graphical dialogs. Essentially the same zenity but closer to kde. And it will fit much better in design to Dolphin.

There is no point in describing simple functions such as burst, extract and the like. Everything is simple and clear there.

Of interest is the remove function which removes a range of pages from a document. pdfkt does not support deleting pages. But it can extract pages both one at a time and a range of pages. This trick is what we use to remove a range of pages.

First of all, you need to define the range of pages. We ask the user through kdialog for the range boundaries. And we determine the number of the last page in the document. It is needed so as not to go beyond the boundaries of the document.

pdf_service_menu: kdialog – page ranges
msg_remove_title="Удалить страницы..."
msg_remove_range="Диапазон страниц для удаления из документа\n\"${filename}\"\n\nИнструкция:\n\nКлючевое слово \"end\" может использоваться, чтобы сослаться на заключительную\nстраницу документа, вместо номера страницы. Сошлитесь на одну страницу, опуская\nномер конечной страницы.\n\nПримеры:\n\n2 - удалить страницу 2;\n3-45 - удалить страницы с 3 по 45;\n5-end - удалить страницы с 5 по последнюю.\n\nВведите диапазон страниц для удаления:"

range="$(kdialog --icon viewpdf --title "${msg_remove_title}" --inputbox "${msg_remove_range}" "2-end")" || return 1

. . .

totalpages="$(pdftk "${@}" dump_data | grep "NumberOfPages" | cut -d" " -f 2-)"

Then we check the start and end page ranges. We need to make sure that we do not go beyond the boundaries of the document.

pdf_service_menu: page ranges
if [ "${first}" = "end" ]; then
	if [ -z "${first}" ]; then
		if [ "${first}" -eq 0 ]; then
		elif [ "${first}" -gt "${totalpages}" ]; then

. . .

if [ "${last}" = "end" ]; then
	if [ -z "${last}" ]; then
		elif [ "${last}" -lt "${first}" ]; then
		elif [ "${last}" -gt "${totalpages}" ]; then

After all the preparatory operations, we determine the boundaries of the ranges for extracting pages.

pdf_service_menu: page ranges to retrieve
# определяем диапазон страниц range1
if [ "${first}" -eq 1 ]; then
	elif [ "${first}" -eq 2 ]; then
			range1="1-$((first - 1))"
# определяем диапазон страниц range2
if [ "${last}" -eq "${totalpages}" ]; then
	elif [ "${last}" -eq "$((totalpages - 1))" ]; then
		range2="$((last + 1))-${totalpages}"

Next, we check that we do not delete all pages from the document, we indicate where to save the final file. And we perform the extraction of pages in the found ranges.

Connecting scripts to the context menu

The scripts are ready, it remains to connect them to the Dolphin context menu. We write a simple desktop file in which we paint our functions and throw it into the ServiceMenus directory.


All scripts and functions were written exclusively from the tasks that most often had to be solved. stamp was added later for experimental purposes and has remained. info – also does not add much functionality. And all other functions are used quite often and are in demand. Before using these scripts, you need to make sure that all the packages used in the scripts are installed. pdftk, for example, must be installed separately.

All scripts and desktop file are available at github. There is also a simple script for copying scripts to user directories.

Similar Posts

Leave a Reply