Skip to content

Commit

Permalink
Create pdfless plugin files and main scripts
Browse files Browse the repository at this point in the history
Also add LICENSE, README, logo and favicon
  • Loading branch information
shivaprsd committed Dec 17, 2020
0 parents commit 4056112
Show file tree
Hide file tree
Showing 18 changed files with 528 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web
build
LICENSE
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Shiva Prasad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
![pdfless logo](plugin/images/logo-small.png)

# pdfless

*pdfless* is a customization plugin for [PDF.js][1] web viewer.
It adds a switch for **Terminal mode** where you can change the colors and font
of (non-scanned) PDF files. Read it as if it's on a terminal screen!

## Installation

1. [Download][2] the latest version (or clone this repo)
2. `cd` into the root directory and run `./install.sh`
3. Follow onscreen instructions

## Notes

Define your colors in `./plugin/colors.json` Default colors are taken from the
[Smyck][3] colorscheme.

Turning images off will improve performance and reduce overhead while switching
between Terminal mode and normal view.

*pdfless* does not attempt text-reflow (which is hopeless in PDFs). Resizing the
text changes only the font-size without touching their position. This may result
in disjoint / overlapping text; **this is not a bug**.

## Why *pdfless*?

I love reading on the terminal. The dark background, the colors, the monospaced
font are all great for the eyes. But the stuff I have to read mostly are all in
PDFs. PDFs are great for printing; but for reading, they suck. Bright white with
dark text, fixed and rigid. Simply inverting the color is not a great solution;
the font might still suck and images turn into their negative ghosts.

PDF.js renders PDFs as HTML, enabling styling via CSS. *pdfless* adds a custom
mode to the default viewer with controls to modify these styles on the go. Thus,
it makes the PDF *less* rigid and suck *less* while reading. The name is also a
shout-out to the Unix file-viewer utility `less`.

There are PDF readers that does this in other ways (including Adobe reader), but
I couldn't find one satisfactory. Many of them are also bloatwares (especially
Adobe reader) and none of them goes as far as changing the font. PDF.js is small
and runs decently in most modern browsers, requiring no additional software.

[1]: https://mozilla.github.io/pdf.js/
[2]: https://github.com/shivaprsd/pdfless/releases/latest
[3]: https://github.com/hukl/Smyck-Color-Scheme
Binary file added favicon.ico
Binary file not shown.
47 changes: 47 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
pdfjs_ver="2.6.347"
zip="pdfjs-$pdfjs_ver-dist.zip"
url="https://github.com/mozilla/pdf.js/releases/download/v$pdfjs_ver/$zip"
cmd_exist() {
command -v "$@" >/dev/null 2>&1
}

cat <<'EOF'
____ ____ ____ __ ____ ____ ____
( _ \( \( __)( ) ( __)/ ___)/ ___)
) __/ ) D ( ) _) / (_/\ ) _) \___ \\___ \
(__) (____/(__) \____/(____)(____/(____/
pdfless is a customization plugin for PDF.js web viewer.
It adds a switch for 'Terminal mode' where you can change the colors and font
of (non-scanned) PDF files. Read it as if it's on a terminal screen!
EOF
if ! [ -f web/viewer.html ]; then
echo "Downloading latest PDF.js prebuilt beta ($pdfjs_ver)..."
if ! [ -f "$zip" ]; then
if cmd_exist wget; then
wget -q --show-progress "$url"
elif cmd_exist curl; then
curl -fLO# "$url"
else
echo "Download failed! Manually download PDF.js and try again."
exit 1
fi
fi
printf "Extracting..."
unzip -q "$zip" || exit
echo "done."
rm -f "$zip"
rm -f web/compressed.tracemonkey-*
fi

grep -q 'pdfless.js' web/viewer.html && echo "Already installed!" && exit
printf "Installing pdfless..."
sed -e 's/\(<\/head>\)/<script src="..\/..\/plugin\/pdfless.js"><\/script>\1/' \
web/viewer.html > tmp && mv tmp web/viewer.html
printf "done.\n\n"
echo "Installation completed! Run \`./pdfless -h\` for usage info."
echo "To undo this installation, run \`rm -r build web LICENSE\`."
echo "You may move this directory anywhere, preserving its internal structure."
echo "Optionally add it to your PATH or create a symlink to ./pdfless in PATH."
73 changes: 73 additions & 0 deletions pdfless
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh
usage() {
printf "Usage: pdfless [-h] <browser|url-opener> [<file>]\nArguments:\
\n\tbrowser|url-opener: A browser (e.g. \`firefox\`) or URL opener\
\n\t(e.g. \`xdg-open\`) to open the <file>.\n" 1>&2;
}
cmd_exist() {
command -v "$@" >/dev/null 2>&1
}
get_path() {
if [ -h "$1" ]; then
dirname "$(readlink "$1")"
else
dirname "$1"
fi
}
start_server() {
if cmd_exist python3; then
serve="python3 -m http.server $1"
elif cmd_exist python2; then
serve="python2 -m SimpleHTTPServer $1"
elif cmd_exist ruby; then
serve="ruby -run -e httpd . -p $1"
elif cmd_exist php; then
serve="php -S localhost:$1"
else
echo "pdfless: Cannot run server."
exit 1
fi
cd "$2" >/dev/null 2>&1 || exit
$serve >/dev/null 2>&1 &
trap "rm -rf tmp >/dev/null 2>&1; kill $!" EXIT
trap "exit 0" INT TERM
echo "pdfless: Server running at localhost:$1. ^C to quit."
sleep 1
}

while getopts "h" o; do
case "$o" in
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))

if [ -z "$1" ] || ! cmd_exist "$1"; then
usage
exit 1
fi
exepath="$(get_path "$0")"
mkdir -p "$exepath/tmp" || exit
if [ -n "$2" ]; then
if ! [ -f "$2" ]; then
echo "pdfless: No such file: $2"
exit 1
else
cp "$2" "$exepath/tmp/" || exit
fname="$(basename "$2")"
fpath="../tmp/$fname"
echo "pdfless: Opening $fname in $1..."
fi
fi

port=60656 # Random port in private range
nc -z localhost $port >/dev/null 2>&1 || start_server $port "$exepath"
$1 "http://localhost:$port/web/viewer.html?file=$fpath"
wait
23 changes: 23 additions & 0 deletions plugin/colors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"textColors": {
"Bright White": "#F7F7F7",
"White": "#B0B0B0",
"Gray": "#5D5D5D",
"Red": "#C75646",
"Green": "#8EB33B",
"Yellow": "#D0B03C",
"Blue": "#4E90A7",
"Magenta": "#C8A0D1",
"Cyan": "#218693",
"Bright Red": "#E09690",
"Bright Green": "#CDEE69",
"Bright Yellow": "#FFE377",
"Bright Blue": "#9CD9F0",
"Bright Magenta": "#FBB1F9",
"Bright Cyan": "#77DFD8"
},
"termColors": {
"background": "#242424",
"highlight": "rgba(33, 134, 147, 0.6)"
}
}
Binary file added plugin/images/logo-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plugin/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/secToolbarButton-colorInput-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/secToolbarButton-colorInput.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/secToolbarButton-fontInput-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/secToolbarButton-fontInput.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/toolbarButton-termMode-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions plugin/images/toolbarButton-termMode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions plugin/pdfless.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
:root {
/* Fallbacks */
--canvasDisplay: none;
--termFont: none;
--termColor: #f7f7f7;
--termBG: #242424;
--termHL: #b0b0b0;
--termMode-icon: url(../plugin/images/toolbarButton-termMode.svg);
--fontInput-icon: url(../plugin/images/secToolbarButton-fontInput.svg);
--colorInput-icon: url(../plugin/images/secToolbarButton-colorInput.svg);
--secToolbarWidth: 220px;
--secToolbarBtnPad: 36px;
--secToolbarIconLeft: 12px;
}
@media (prefers-color-scheme: dark) {
:root {
--termMode-icon: url(../plugin/images/toolbarButton-termMode-dark.svg);
--fontInput-icon: url(../plugin/images/secToolbarButton-fontInput-dark.svg);
--colorInput-icon: url(../plugin/images/secToolbarButton-colorInput-dark.svg);
}
}

#fontInput {
max-width: calc(100% - 92px);
margin-right: 2px;
vertical-align: top;
}
#colorInput {
width: calc(100% + 22px);
height: 26px;
}
#colorInput:focus {
border-color: rgba(0, 0, 0, 0.38);
}
#colorSelectContainer {
position: relative;
max-width: calc(var(--secToolbarWidth) / 2 - 4px);
}

.fontControls, .colorControl {
position: relative;
padding-left: var(--secToolbarBtnPad);
margin-bottom: 4px;
}
.colorControl, .imageControl {
display: inline-block;
}
.imageControl {
position: relative;
margin-left: 6px;
overflow: hidden;
}
.imageControl label {
display: block
}
.imageControl label:hover {
background-color: var(--button-hover-color);
}
.imageControl .toolbarField[type="checkbox"]:checked + .toolbarLabel {
background-color: var(--toggled-btn-bg-color);
}

.fontControls::before, .colorControl::before {
position: absolute;
top: 8px;
left: var(--secToolbarIconLeft);
}
.fontControls::before {
content: var(--fontInput-icon);
}
.colorControl::before {
content: var(--colorInput-icon);
color: var(--termColor) !important;
}
.toolbarButton.termMode::before, .secondaryToolbarButton.termMode::before {
content: var(--termMode-icon);
}
61 changes: 61 additions & 0 deletions plugin/pdfless.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<template id="toolbarAddon">
<button id="termMode" class="toolbarButton termMode hiddenLargeView" title="Toggle Terminal Mode" tabindex="30">
<span>Terminal Mode</span>
</button>
</template>

<template id="secToolbarAddon">
<div class="fontControls">
<input id="fontInput" class="toolbarField" placeholder="Terminal Font">
<div class="splitToolbarButton">
<button id="fontReduce" class="toolbarButton zoomOut" title="Reduce Fontsize">
<span>A-</span>
</button>
<div class="splitToolbarButtonSeparator"></div>
<button id="fontEnlarge" class="toolbarButton zoomIn" title="Increase Fontsize">
<span>A+</span>
</button>
</div>
</div>
<div class="colorControl">
<div id="colorSelectContainer" class="dropdownToolbarButton">
<select id="colorInput" class="toolbarField" title="Text Color"></select>
</div>
</div
><div class="imageControl">
<input type="checkbox" id="imageEnable" class="toolbarField" checked
><label for="imageEnable" class="toolbarLabel">Images</label>
</div>
<div class="horizontalToolbarSeparator"></div>
<button id="secTermMode" class="secondaryToolbarButton termMode visibleLargeView" title="Toggle Terminal Mode" tabindex="50">
<span>Terminal Mode</span>
</button>
</template>

<style id="termModeTheme">
.page {
background-color: var(--termBG) !important;
}
.textLayer {
opacity: 1;
background-color: transparent;
}
.textLayer > span, .colorControl::before {
color: var(--termColor) !important;
}
.canvasWrapper {
display: var(--canvasDisplay);
}
::selection, .textLayer ::selection {
background-color: var(--termHL);
}
::-moz-selection {
background-color: var(--termHL);
}
</style>

<style id="termModeFont">
.textLayer > span {
font-family: var(--termFont) !important;
}
</style>
Loading

0 comments on commit 4056112

Please sign in to comment.