Coverage for gws-app/gws/lib/pdf/__init__.py: 0%
39 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1import pypdf
4def overlay(a_path: str, b_path: str, out_path: str) -> str:
5 """Overlay two pdfs page-wise.
7 Args:
8 a_path: Path to pdf a.
9 b_path: Path to pdf b, which will be placed on top.
10 out_path: Path to the output pdf.
12 Returns:
13 Path to the output pdf.
14 """
16 fa = open(a_path, 'rb')
17 fb = open(b_path, 'rb')
19 ra = pypdf.PdfReader(fa)
20 rb = pypdf.PdfReader(fb)
22 w = pypdf.PdfWriter()
24 for n, page in enumerate(ra.pages):
25 other = None
26 try:
27 other = rb.pages[n]
28 except IndexError:
29 pass
30 if other:
31 # https://github.com/py-pdf/pypdf/issues/2139
32 page.transfer_rotation_to_content()
33 page.merge_page(other)
34 w.add_page(page)
36 with open(out_path, 'wb') as out_fp:
37 w.write(out_fp)
39 fa.close()
40 fb.close()
42 return out_path
45def concat(paths: [str], out_path: str) -> str:
46 """Concatenates multiple pdfs into one.
47 The order of the pages are the same as the order of the paths in the list.
49 Args:
50 paths: Paths to the pdfs.
51 out_path: Path to the output pdf.
53 Returns:
54 Path to the concatenated pdf.
55 """
57 # only one path given - just return it
58 if len(paths) == 1:
59 return paths[0]
61 # NB: readers must be kept around until the writer is done
63 files = [open(p, 'rb') for p in paths]
64 readers = [pypdf.PdfReader(fp) for fp in files]
66 w = pypdf.PdfWriter()
68 for r in readers:
69 w.append_pages_from_reader(r)
71 with open(out_path, 'wb') as out_fp:
72 w.write(out_fp)
74 for fp in files:
75 fp.close()
77 return out_path
80def page_count(path: str) -> int:
81 """Returns the amount of pages for a given pdf.
83 Args:
84 path: Path to the pdf.
86 Returns:
87 Amount of pages in the pdf.
88 """
89 with open(path, 'rb') as fp:
90 r = pypdf.PdfReader(fp)
91 return len(r.pages)
93# def to_image(in_path, out_path, size, mime):
94# if mime == gws.lib.mime.PNG:
95# device = 'png16m'
96# elif mime == gws.lib.mime.JPEG:
97# device = 'jpeg'
98# else:
99# raise ValueError(f'uknown format {format!r}')
100#
101# cmd = [
102# 'gs',
103# '-q',
104# f'-dNOPAUSE',
105# f'-dBATCH',
106# f'-dDEVICEWIDTHPOINTS={size[0]}',
107# f'-dDEVICEHEIGHTPOINTS={size[1]}',
108# f'-dPDFFitPage=true',
109# f'-sDEVICE={device}',
110# f'-dTextAlphaBits=4',
111# f'-dGraphicsAlphaBits=4',
112# f'-sOutputFile={out_path}',
113# in_path,
114# ]
115#
116# gws.log.debug(repr(cmd))
117# gws.lib.osx.run(cmd)
118#
119# return out_path