Skip to content

Commit

Permalink
add funciton of hosting a pdf file on local computer
Browse files Browse the repository at this point in the history
  • Loading branch information
monado3 committed May 14, 2020
1 parent fd38f7d commit f09f176
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
pdfjs-broker
---
CLI-tool which opens a pdf file specified by command line argument in the form of URI with pdf.js.
CLI-tool which opens a pdf file specified by command line argument in the form of URL or local filepath with pdf.js.

# Requirements
- docker environment
- docker environment (You have to be able to execute docker commands without `sudo`)
- Python 3 (>=3.6)

# Installation
1. `$ git clone {this repository}`
1. `$ cd pdfjs_server_url && docker build -t pdfjs_url .`
1. Add $BROWSER={your browser you want to use for reading pdf} to environment variable
1. `$ ln -s {/foo/broker.py} /usr/local/bin/pdfjs
1. `$ cd ../pdfjs_server_file && docker build -t pdfjs_file .`
1. Add $BROWSER={your browser you want to use for reading pdf files} to environment variable
1. `$ ln -s {/foo/broker.py} /usr/local/bin/pdfjs`

# Usage
`$ ./broker.py {URL of pdf file you want to read}`

*I recommend setting the alias to `broker.py`*
`$ pdfjs [-u <URL of a pdf file> | -f <pdf file path>]`
22 changes: 14 additions & 8 deletions broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import argparse
import socket
import subprocess as subp
import sys

from pathlib import Path

def is_free(port_n: int) -> bool:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand All @@ -14,24 +15,28 @@ def is_free(port_n: int) -> bool:
return False


def search_min_open_port(n_min: int):
def search_min_open_port(n_min: int) -> int:
for i in range(n_min, 60000):
if is_free(i):
return i


def exec_pdfjs_ctn_url(pdf_url:str, n_port:int):
proc = subp.Popen(['docker', 'run', '-itd', '--rm', '-p', f'{n_port}:8080', 'pdfjs_url', pdf_url], stdout=subp.DEVNULL, stderr=subp.DEVNULL)
proc = subp.Popen(['docker', 'run', '-itd', '--rm', '-p', f'{n_port}:8080', '--name', f'pdfjs_{n_port}', 'pdfjs_url', pdf_url], stdout=subp.DEVNULL, stderr=subp.DEVNULL)


def exec_pdfjs_ctn_file(pdf_path:str, n_port:int):
proc = subp.Popen(['docker', 'run', '-itd', '--rm', '-p', f'{n_port}:8080', 'pdfjs_file'], stdout=subp.DEVNULL, stderr=subp.DEVNULL)
proc = subp.Popen(['docker', 'cp'])
ctn_name = f'pdfjs_{n_port}'
proc = subp.Popen(
['docker', 'run', '-itd', '--rm', '-p', f'{n_port}:8080', '-v', f'{pdf_path}:/pdfjs/web/downloaded.pdf', '--name', ctn_name, 'pdfjs_file'],
stdout=subp.DEVNULL,
stderr=subp.DEVNULL
)

def init_argparser():
parser = argparse.ArgumentParser(
prog='broker.py',
usage='pdfjs [-u <URL>] [-f <filepath>]',
usage='pdfjs [-u <URL> | -f <filepath>]',
description='Open a pdf in a broweser by pdfjs',
add_help=True,
)
Expand Down Expand Up @@ -59,6 +64,7 @@ def init_argparser():
if args.url:
url = args.url
exec_pdfjs_ctn_url(url, n_port)
subp.run(f'$BROWSER http://localhost:{n_port}/web/viewer.html?file=downloaded.pdf', shell=True, stdout=subp.DEVNULL, stderr=subp.DEVNULL)
else:
path = args.path
filepath = (Path.cwd() / args.path).resolve()
exec_pdfjs_ctn_file(str(filepath), n_port)
subp.run(f'$BROWSER http://localhost:{n_port}/web/viewer.html?file=downloaded.pdf', shell=True, stdout=subp.DEVNULL, stderr=subp.DEVNULL)

0 comments on commit f09f176

Please sign in to comment.