3

I hope the title is not misleading.

I am using an note taking app that let's me open files from markdown notes:

[read more](/path/to/file.pdf)

I couldn't find a way to open it at a specific page. I cannot create a link in The format which the PDF reader accepts.

Is there a good way I can open files with a script? Basically I want to replace #page=5 with -p 5 every time.

I thought I could write my own wrapper script such that I can type:

[read more](/path/to/file.pdf#page=5)

and my script parses the path and transforms it into

 xdg-open /path/to/file.pdf -p 5
3

I'm not sure how elegant is the method below. But it works normally for me.

I'm using ReText as Markdown editor and plan to use

[link-to-pdf](pdf-filename.pdf#page=2)

syntax for PDF with page number.

So I have defined my local xdg-open wrapper script:

mkdir /home/$USER/bin
echo "export PATH=/home/$USER/bin:$PATH" >> .bashrc

and placed the following code to /home/$USER/bin/xdg-open:

#!/bin/sh

pdfviewer=atril
filename="$1";

case $filename in
    *.pdf#page=*) 
            file=$(echo "$filename" | cut -d "#" -f 1);
            page=$(echo "$filename" | cut -d "#" -f 2 | sed "s/page=/-p /");
            $pdfviewer "$file" $page ;;
    *)      /usr/bin/xdg-open "$@" ;;
esac

This script should be executable (set with chmod +x /home/$USER/bin/xdg-open), and it will be used only if .pdf#page= will be found, otherwise it will use system-wide /usb/bin/xdg-open.

I have tested this method with Atril PDF viewer on my Ubuntu 16.04 LTS MATE.
You can change pdfviewer variable to evince if you want.

As free bonus we can use the .pdf#page= syntax in the terminal:

xdg-open /usr/share/doc/qpdf/qpdf-manual.pdf#page=12

Note: after such manipulations we have two xdg-open executables - the output of whereis xdg-open should become xdg-open: /usr/bin/xdg-open /home/user/bin/xdg-open /usr/share/man/man1/xdg-open.1.gz.

  • +1 but usually $@ is double quoted "$@" I think. Same should apply for "$" and "$filename" I believe. Otherwise filenames with a space would be considered two different file names split on the space. – WinEunuuchs2Unix Oct 14 '18 at 22:26
  • @linuxUser123 Thanks for confirmation. N0bert the "$" above should read "$1". Have too many things on the go tonight :) – WinEunuuchs2Unix Oct 14 '18 at 22:35
  • @WinEunuuchs2Unix thanks. See edit above. Is it enough / correct? – N0rbert Oct 14 '18 at 22:36
  • @N0rbert Welcome. Looks perfect to my three beer eyes :) – WinEunuuchs2Unix Oct 14 '18 at 22:38
  • @linuxUser123 fixed :) – N0rbert Oct 14 '18 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.