You are not logged in.

#1 2019-10-16 17:05:07

stefano
Member
Registered: 2011-04-09
Posts: 287

[SOLVED] Are there any scriptable pdf readers for Linux?

I am trying to hack something together that can replicate locally the behaviour of the ReadLang webapp (a free foreign language study tool) for local pdf files. Readlang's basic functionalities are the following:

1. Load a file in e-pub format from local or web sources and show it on screen
2. Once the user selects a portion of text, let the user see a translation (using google translate)
3. Automatically create a flashcard with the selected/searched-for text that includes the text's context   
4. Allow user to view and study flashcards with the usual spaced repetition algorithms

Almost all the needed tools are  available:

a. Anki---a flashcard app that offer a REST api run through a minimal local server. One of the API's actions allows creating a new flashcard with custom text and parameters (including front/back/context/audio, and so on).
b. bash/curl/jq ---glue code and tools to create and send the JSON object for the new card to Anki.

These tools/apps take care of point 4 above and of the receiving part of point 3 (card creation from external program). Loading a pdf file (point 1) is the routine job of any pdf reader. That leaves point 2---showing a translation---and the "emitting" part of point 3---sending selected text to the proper shell's command.

Ideally, a scriptable pdf reader would fit the bill. It would call an external command and pass the selected text to it. Okular offers half of this functionality (point 2 above) through KDE's web shortcuts:  they allow the user to open a predefined web page (such as dictionaries) passing the selected text as a URI parameter.  Interfacing with Anki's API is not possible though, since you cannot create a HTTP POST with a URI. I would take a  reader with a programmable/scriptable interface and with access to pdf's objects (selection, etc.).

My search has so far proved unsuccessful. I tried to put together something with emacs and its pdftools pdf reader (which is a sub-optimal solution compared to modern readers such as Okular and similar, but one I could live with), but I could not get the pdf viewer to properly interface with the standard shell-command-on-region command. I read that Adobe Acrobat has a rather extensive javascript support, even though it does not seem to include the equivalent of a document.getSelection function. Besides, we are stuck at version 9 on Linux and the Adobe website page seems to have removed documentation for that version's JS support.

Suggestions?

Last edited by stefano (2020-03-31 14:43:07)

Offline

#2 2019-10-20 10:40:24

andym
Member
From: France
Registered: 2019-01-16
Posts: 61

Re: [SOLVED] Are there any scriptable pdf readers for Linux?

I think this may be a big ask smile

Firstly, the pdf may be in text form or an image. If it is the latter then you will need some ocr software to capture the text. Assuming it is the former then, for example, okular will allow the user to select some text and copy the selection to the clipboard. If the text or image selected were to be saved to a file of known filename or folder then you could use inotifywait to se the capture and then run your programme.

Offline

#3 2019-10-20 13:47:37

stefano
Member
Registered: 2011-04-09
Posts: 287

Re: [SOLVED] Are there any scriptable pdf readers for Linux?

It turns out there is a scriptable pdf reader for Linux and it is....emacs! Emacs with the pdf-tools package is a quite usable pdf reader and of course it has elisp behind it. pdf-tools allows the user to select text---like Okular does---and makes it available to elisp functions, which include the possibilty to launch en external process on the selected text. Even for a total elisp newbie like me it was enough---with the help of the developer I could hack together a few lines of code I could get emacs to talk to the shell and then to the Anki server.  So my basic problem is solved. It would be nice to get the translation as well, but I have not been able to find a public dictionary server that returns an easy to parse object. Admittedly, I haven't looked very hard yet.  It seems WordReference could do the trick, but either they have discontinued their API key program or it's been hidden so well I could not find it.

@andym: The "solution" I found assumes the pdf file contains text or is a double layered image+OCR'ed text pdf sandwich. The latter goal is often not too hard to achieve with a combination of freely available tools centered around Google's tesseract. and pdf-sandwich.  For a purely graphic Pdf there is no solution, of course.

Offline

Board footer

Powered by FluxBB