-1

I would like to make a call to a local server running a REST interface from within a pdf reader, passing the selected text as argument. The first option that came to mind was to write a simple bash script with a curl call inside, and use a script from within the pdf app to execute it.

Are there any scriptable pdf readers that would allow that? It seems that none of the most common ones (e.g. Okular, Evince, gv, xpdf) would make it possible.

Added after @DanielH comment:

I'm not asking for a method to launch arbitrary code when the a pdf file is opened. Rather, I am asking if the user can choose to launch an external app from within the pdf reader (or script).

Okular (KDE reader) has a limited form of this functionality: using KDE web shortcuts, the user can select a portion of text and then launch a browser with a predefined url that contain the text in the URL.

For instance, when an Okular user selects a word and then chooses the "Google" shortcut, Okular will launch a browser and put the following pseudo-URI in the location bar:

https://www.google.com/search?q=\{@}&ie=UTF-8

with {@} replaced by the selected text. KDE Web shortcuts are user-editable and can result in rather complex queries.

This functionality is very close to what I am after, except that the only http request that can be managed this way (as far as I know), is GET. Instead, I would need to start a POST http request, which as far as I know cannot be done from the location bar. Hence, my question about using using bash+curl.

stefano
  • 769
  • 2
  • 10
  • 24
  • If I understand the question right, that would be considered a serious security flaw. Opening a PDF is not supposed to be able to run arbitrary code on the computer. It *is* possible to put Javascript in PDFs, but I don't think it's given access to external resources and I don't know much about it. – Daniel H Oct 14 '19 at 20:00
  • @DanielH See more detailed explanation – stefano Oct 14 '19 at 21:12
  • I'm still pretty sure you can't do it *from within the PDF file*, but if you're willing to ship the shell script separately and ask people to run that when reading the PDF it may be possible. – Daniel H Oct 15 '19 at 05:55
  • Apparently PDF JavaScript does let you send POST requests but doesn't give you access to the selection, and anything not written in PDF JavaScript has to be separate for the security reasons. If you're willing to do that I expect it's possible; people who write LaTeX try to make sure PDF readers have good APIs for other programs on the system. – Daniel H Oct 15 '19 at 06:00
  • It turns out the answer was simple: use emacs with pdf-tools. emacs is of course scriptable (the understatement of the century), and with the pdf-tools package is is a very usable reader. – stefano Oct 16 '19 at 06:12

1 Answers1

0

I should have known...anything related to text on Unix? The answer is always emacs.

In particular:

  1. the pdf-tools package can be used to read pdf files in an emacs buffer. It is much more efficient than PdfView and gives full access to annotations and all the usual emacs editing command (selecting regions, etc).

  2. The standard emacs command shell-command-on-region can be used to launch a command with the selected region passed to it as standard input.

  3. The command (as always in emacs) can be called interactively or from an emacs function.

  4. All it's needed is to write a simple shell script that wraps up the needed command and passes on the input it receives.

In my case, I needed to call curl with a few parameters and a JSON string encapsulating the POST request. Iencountered a minor problem wince emacs passes the selected text (region) as standard input and not as argument, but this SX question (and @andy answer especially) helped me understand how to read piped standard input into a variable.

stefano
  • 769
  • 2
  • 10
  • 24