-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_join
executable file
·70 lines (51 loc) · 1.53 KB
/
pdf_join
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
import json
import shlex
import subprocess as sp
from pathlib import Path
from typing import List
import typer
from rich import print as echo
app = typer.Typer(pretty_exceptions_show_locals=False)
@app.command()
def main(
files: List[Path],
outfile: Path = "joined.pdf",
outfile_prefix: str = "tmp_",
outfile_suffix: str = "pdf",
):
_outfiles = []
for ii, file in enumerate(files):
echo(file)
tag = f'"{file}"'
echo("... Tag:")
echo(tag)
_outfile = f"{outfile_prefix}{ii:03d}.{outfile_suffix}"
cmd = f'echo "{tag}"'
ps1 = sp.Popen(shlex.split(cmd), stdout=sp.PIPE)
cmd = "enscript -B -f Courier-Bold32 -o-"
ps2 = sp.Popen(shlex.split(cmd), stdin=ps1.stdout, stdout=sp.PIPE)
cmd = f'ps2pdf - "{_outfile}" '
sp.run(shlex.split(cmd), stdin=ps2.stdout)
ps1.wait()
ps2.wait()
cmd = f'qpdf {file} --overlay "{_outfile}" -- {_outfile}'
sp.run(shlex.split(cmd))
_outfiles.append(_outfile)
_cmd = "pdfjam "
for _outfile in _outfiles:
_cmd += f"{_outfile} '-' "
_cmd += "--landscape "
_cmd += f"--outfile {outfile}"
# check if files actually exist
for _file in _outfiles:
assert Path(_file).exists(), f"{_file} does not exist, arguments are folders!"
echo("... run:")
echo(_cmd)
sp.run(shlex.split(_cmd))
# cleanup
for _file in _outfiles:
Path(_file).unlink()
echo("done.")
if __name__ == "__main__":
app()