dan
dan

Reputation: 45652

how do you open a PDF at a specific page from the command line? (OSX or Linux)

I want to open a PDF document at a specific page from the command line, sort of like vim +n [file]. Is there any way to do that in OSX, with any PDF reader program?

Upvotes: 13

Views: 9499

Answers (9)

K J
K J

Reputation: 11877

In the past the answer would have been

AppleScript solutions with Preview are quite limited.

The most common method to open a PDF Today is a browser, rather than Acrobat as it was at the time of the question when then the correct ALL PLATFORMS answer was:

/path to /Acrobat_Reader /A "page=2" somePDFFile.pdf

Currently across platforms many browsers can open a PDF.

For example:

Chrome "/path/filename.pdf#page=2"

However the exact prefix for mounting a drive may vary so here is a windows example. >chrome "file://c:/path/PDFDec_s.pdf#page=2"

enter image description here

Upvotes: 0

Vlad
Vlad

Reputation: 6730

In Skim.app on macOS by using JXA:

const app = Application("net.sourceforge.skim-app.skim");
const activeDocument = app.windows[0].document(); // Active document
const pages = activeDocument.pages();
const targetPage = pages[2]; // Use desired index instead of `2`
activeDocument.go({ to: targetPage });

Upvotes: 0

Greg Marks
Greg Marks

Reputation: 183

In Linux you can open a PDF file (say, file.pdf) to a certain page (say, page 23) using Okular, mupdf, qpdfview, or gv, with the respective commands:

 okular -p 23 file.pdf

 mupdf file.pdf 23

 qpdfview file.pdf#23

 gv -page=23 file.pdf

Upvotes: 0

William Denman
William Denman

Reputation: 3164

I kept getting errors from Julio's answer. With the current Version 1.4.6 (80) of Skim and OSX Mountain Lion (10.8.5), the following code worked for me. I think the issue might be that pageNum has to be treated as an integer, but it gets parsed in as a string. This code assumes you have Skim installed.

gotopage.scpt:

on run argv
    set fileName to (item 1 of argv)
    set pageNum to (item 2 of argv) as integer

    tell application "Skim"
        open fileName
        tell document 1 to go to page pageNum
        activate
    end tell
end run

Skim still needs an absolute filename. So you would run it with the same command mentioned in Julio's answer. osascript gotopage.scpt "/full/path/to/doc/mydoc.pdf" 99

Upvotes: 8

Julio Gorgé
Julio Gorgé

Reputation: 10106

The following method works with Skim, an open-source replacement for Preview.app. First, download Skim, then save the following code on a text file and name it "gotopage.scpt":

on run argv
    tell application "Skim"
        activate
        open (item 1 of argv)

        tell document 1
            go to page index (item 2 of argv)
        end tell

    end tell
end run

You can now tell Skim to open a certain PDF and go to page 99 by writing this on the terminal:

osascript gotopage.scpt "/full/path/to/doc/mydoc.pdf" 99

You might want to wrap the above line into an sh script of your own. Also note that the path to the PDF must be absolute, otherwise Skim won't find the file.

Upvotes: 6

Derek Swingley
Derek Swingley

Reputation: 8752

open -a Preview someFile.pdf

Upvotes: -4

Alex Ntousias
Alex Ntousias

Reputation: 9122

Maybe this will help you: http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf

I found an arg page=pagenum on page 5 in this pdf that might be useful! I haven't tested it though!

EDIT:

I just tested it on Windows (unfortunately I don't have Linux) and it works. On windows it's:

<path to Acrobat Reader.exe> /A "page=2" somePDFFile.pdf

I guess it's something similar in Linux or OSX.

Upvotes: 5

ezod
ezod

Reputation: 7421

You can do this with Evince using the -p or --page-label=PAGE command line argument like so:

evince -p 5 foo.pdf

Upvotes: 8

Daniel A. White
Daniel A. White

Reputation: 191037

No. Command line switches are specific to each program.

Upvotes: -3

Related Questions