-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixhenlepdf.py
47 lines (34 loc) · 1.66 KB
/
fixhenlepdf.py
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
"""Remove margins from PDF generated by Henle app."""
import argparse
from pathlib import Path
from pikepdf import (Array, Pdf, PdfMatrix, parse_content_stream,
unparse_content_stream)
__version__ = '1.0.0'
def main():
parser = argparse.ArgumentParser("fixhenldpdf",
description="Remove margins from Henle notes PDF files")
parser.add_argument("file", type=Path,
help="The original PDF file to fix")
parser.add_argument("--output", "-o", type=Path,
help="Result file where to save printable PDF")
parser.add_argument("--scale", type=float, default=1.19,
help="Scale (default %(default)s)")
parser.add_argument("--xoff", type=float, default=10,
help="Offset X by (default %(default)s)")
parser.add_argument("--yoff", type=float, default=-50,
help="Offset Y by (default %(default)s)")
args = parser.parse_args()
if args.output is None:
args.output = args.file.with_stem(args.file.stem + '_print')
with Pdf.open(args.file) as pdf:
for page in pdf.pages:
x = page.MediaBox[2]
y = page.MediaBox[3]
content = parse_content_stream(page)
original = PdfMatrix(content[4][0])
new = original.translated(-x*(args.scale-1)/2+args.xoff, -y*(args.scale-1)/2+args.yoff).scaled(args.scale, args.scale)
content[4] = Array([*new.shorthand]), content[4][1]
page.Contents = pdf.make_stream(unparse_content_stream(content))
pdf.save(args.output)
if __name__ == '__main__':
main()