Sample Header Ad - 728x90

Display pdftohtml output using elinks -remote option?

1 vote
0 answers
235 views
The following command works great to view pdf content from the command line: pdftohtml -i -stdout file.pdf |elinks. As should be clear, what's happening is that file.pdf is being converted to html and then piped to the text-mode browser elinks for display. What I'm hoping to do though, is a slight variation on this. I want to use elinks' -remote switch to cause the output from the pdftohtml command to be sent to a new tab in an already-running instance of elinks. The way this works, using as an example a URL instead of the output of a command, is as follows: starting elinks in one terminal, then running a command like elinks -remote www.google.com in another terminal, will result in a new tab being opened in the running elinks instance displaying the google search page. I've so far been unable to get a command like this to work on the output of the pdftohtml command. The most straightforward way to do this might seem to be pdftohtml -i -stdout file.pdf |elinks -remote. But based on my experimentation thus far this is not working because the -remote switch evidently needs a URL or a file name to be supplied ("Cannot parse option -remote: Parameter expected"). So my question is whether there is some way to "feed" the output of the pdftohtml to elinks -remote on the fly, as it were? Input will be appreciated. **Things tried so far** I thought maybe a named pipe could help here, but that option is not working. Although something like pdftohtml -i file.pdf my-pipe && elinks succeeds, when I add the -remote switch, it fails. The kludge I've so far gotten to approximate what I'm aiming for is
-i file.pdf /tmp/pdf.html && elinks -remote /tmp/pdf.html && elinks -remote "reload()"
That creates a transitional file(s) in the /tmp directory that proves acceptable to the -remote switch. Reloading the page is required so that any other transitional open page/file the browser might have previously cached is not displayed. It's not really a good resolution because when I try to reload any other tab that might previously have been opened to the transitional page/file, the most recent copy of that page/file will be loaded, not the one that had been previously opened. **Resolution?** Compounding with further kludges, here's a commentated bash script (which I am calling pdf2elinks) I cobbled together, using my modest powers as an internet searcher and my expertise as an elite copy/paster, that addresses the issue of transitional html files produced by pdftohtml being overwritten by subsequent files and thus lost before adequate review. As will be apparent, I decided that assigning numerals rather than names to the converted transitional html files would provide an automated way of assigning unique names. As may also be clear, if one's system is not set up to automatically clear contents of the /tmp directory on reboot, the directory to which the transitional html files are being written will need to be emptied (say, by using a cron script) periodically.
#!/bin/bash
# use: pdf2elinks [filename]
# read the name of the pdf supplied to this script into a variable
pdfname=$1
# test whether the target directory is empty and, if it is, convert the supplied pdf under the name 1.html
if [ ! "$(ls -A /tmp/pdfs2html)" ]; then
pdftohtml -i $pdfname /tmp/pdfs2html/1.html && elinks -remote /tmp/pdfs2html/1.html
else
# if the directory is not empty, find the highest numbered file in /tmp/pdfs2html
number=ls /tmp/pdfs2html/ | sed 's/\([0-9]\+\).*/\1/g' | sort -n | tail -1
# increment by one the highest numbered file found
numberplus=echo "$number +1" | bc
pdftohtml -i $pdfname /tmp/pdfs2html/$numberplus.html && elinks -remote /tmp/pdfs2html/$numberplus.html
fi
In closing, the inspiration for this project of attempting to use elinks as a pager, comes from this page: http://www.pocketnix.org/posts/Life%20on%20the%20command%20line%3A%20Day%20To%20Day%20Console . Like that author, I am often interacting with one of my computers via the command line in an ssh session in which I am running tmux. One of the tabs will always have elinks running in it and it will function as a window where I can open e-mail attachments such as pdf's, read man pages, or just open web pages. A path to the script above has been added to my .mailcap entry for handling pdf's and seems to be performing its function well. I welcome comments, suggestions, improvements, and/or corrections. I function under no illusion that I've found the most optimal solution to the issue I'm trying to address or even that I've understood well what issues are really at stake.
Asked by MJiller (391 rep)
Mar 6, 2021, 04:21 AM
Last activity: Mar 8, 2021, 01:47 PM