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
N0rbert
  • 99,918

1 Answers1

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.

N0rbert
  • 99,918