pdfredact 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfredact/__about__.py +4 -0
- pdfredact/__init__.py +12 -0
- pdfredact/utils.py +219 -0
- pdfredact-0.0.1.dist-info/METADATA +47 -0
- pdfredact-0.0.1.dist-info/RECORD +7 -0
- pdfredact-0.0.1.dist-info/WHEEL +4 -0
- pdfredact-0.0.1.dist-info/licenses/LICENSE.txt +9 -0
pdfredact/__about__.py
ADDED
pdfredact/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2025-present JSv4 <scrudato@umich.edu>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
from .utils import (
|
|
5
|
+
build_text_redacted_pdf,
|
|
6
|
+
redact_pdf_to_images,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"build_text_redacted_pdf",
|
|
11
|
+
"redact_pdf_to_images",
|
|
12
|
+
]
|
pdfredact/utils.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
from PIL import Image, ImageDraw
|
|
7
|
+
from plasmapdf.models.types import OpenContractsSinglePageAnnotationType, PawlsPagePythonType
|
|
8
|
+
from reportlab.lib.colors import Color # For alpha-based transparency
|
|
9
|
+
from reportlab.lib.utils import ImageReader
|
|
10
|
+
from reportlab.pdfgen import canvas
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _compute_pixel_coordinates(
|
|
16
|
+
annotation: OpenContractsSinglePageAnnotationType,
|
|
17
|
+
page_width: float,
|
|
18
|
+
page_height: float,
|
|
19
|
+
image_width: int,
|
|
20
|
+
image_height: int,
|
|
21
|
+
) -> tuple[float, float, float, float]:
|
|
22
|
+
"""
|
|
23
|
+
Convert a PDF (point-based) bounding box to pixel coordinates
|
|
24
|
+
for use with a rasterized image.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
annotation: Dict with 'bounds' containing 'left','right','top','bottom' in PDF coords.
|
|
28
|
+
page_width: The width of the page in PDF points.
|
|
29
|
+
page_height: The height of the page in PDF points.
|
|
30
|
+
image_width: The rasterized image width in pixels.
|
|
31
|
+
image_height: The rasterized image height in pixels.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
(left_px, top_px, right_px, bottom_px) in pixel coordinates.
|
|
35
|
+
"""
|
|
36
|
+
bbox = annotation["bounds"]
|
|
37
|
+
left_px = (float(bbox["left"]) / page_width) * float(image_width)
|
|
38
|
+
right_px = (float(bbox["right"]) / page_width) * float(image_width)
|
|
39
|
+
top_px = (float(bbox["top"]) / page_height) * float(image_height)
|
|
40
|
+
bottom_px = (float(bbox["bottom"]) / page_height) * float(image_height)
|
|
41
|
+
|
|
42
|
+
if top_px > bottom_px:
|
|
43
|
+
top_px, bottom_px = bottom_px, top_px
|
|
44
|
+
|
|
45
|
+
return (left_px, top_px, right_px, bottom_px)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def redact_pdf_to_images(
|
|
49
|
+
pdf_bytes: bytes,
|
|
50
|
+
pawls_pages: list[PawlsPagePythonType],
|
|
51
|
+
page_annotations: list[list[OpenContractsSinglePageAnnotationType]],
|
|
52
|
+
dpi: float = 300.0,
|
|
53
|
+
poppler_path: str | None = None,
|
|
54
|
+
use_pdftocairo: bool = False,
|
|
55
|
+
) -> list[Image.Image]:
|
|
56
|
+
"""
|
|
57
|
+
Convert a PDF to images (via pdf2image) at the specified dpi and apply rectangular
|
|
58
|
+
fill (e.g., black) over bounding boxes. Returns a list of PIL Images.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
pdf_bytes: Raw PDF bytes.
|
|
62
|
+
pawls_pages: PawlsPagePythonType data including page dimension & token info.
|
|
63
|
+
page_annotations: Each page's bounding boxes for redaction.
|
|
64
|
+
dpi: Rasterization resolution in DPI (default 300).
|
|
65
|
+
poppler_path: If set, path to poppler installation (for Windows).
|
|
66
|
+
use_pdftocairo: If True, use pdftocairo instead of pdftoppm.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
List of PIL Images (redacted).
|
|
70
|
+
"""
|
|
71
|
+
from pdf2image import convert_from_bytes
|
|
72
|
+
|
|
73
|
+
pages = convert_from_bytes(
|
|
74
|
+
pdf_bytes,
|
|
75
|
+
dpi=int(dpi),
|
|
76
|
+
poppler_path=poppler_path,
|
|
77
|
+
use_pdftocairo=use_pdftocairo,
|
|
78
|
+
fmt="png",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
redacted_images: list[Image.Image] = []
|
|
82
|
+
|
|
83
|
+
for idx, page_img in enumerate(pages):
|
|
84
|
+
draw = ImageDraw.Draw(page_img)
|
|
85
|
+
page_w = float(pawls_pages[idx]["page"]["width"])
|
|
86
|
+
page_h = float(pawls_pages[idx]["page"]["height"])
|
|
87
|
+
|
|
88
|
+
annots_for_page = page_annotations[idx]
|
|
89
|
+
for annot in annots_for_page:
|
|
90
|
+
left, top, right, bottom = _compute_pixel_coordinates(
|
|
91
|
+
annot,
|
|
92
|
+
page_width=page_w,
|
|
93
|
+
page_height=page_h,
|
|
94
|
+
image_width=page_img.width,
|
|
95
|
+
image_height=page_img.height,
|
|
96
|
+
)
|
|
97
|
+
draw.rectangle([left, top, right, bottom], fill="black")
|
|
98
|
+
|
|
99
|
+
redacted_images.append(page_img)
|
|
100
|
+
|
|
101
|
+
return redacted_images
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def build_text_redacted_pdf(
|
|
105
|
+
output_pdf: str,
|
|
106
|
+
redacted_images: list[Image.Image],
|
|
107
|
+
pawls_pages: list[PawlsPagePythonType],
|
|
108
|
+
page_redactions: list[list[OpenContractsSinglePageAnnotationType]],
|
|
109
|
+
dpi: float,
|
|
110
|
+
hide_text: bool = True,
|
|
111
|
+
) -> None:
|
|
112
|
+
"""
|
|
113
|
+
Build a new PDF from redacted raster images plus a text layer for copy/paste,
|
|
114
|
+
omitting tokens that fall under redacted bounding boxes. The text can be
|
|
115
|
+
fully transparent so it does not obscure the PDF.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
output_pdf: Path to the resulting PDF file.
|
|
119
|
+
redacted_images: List of PIL Images (one per page) with blacked-out areas.
|
|
120
|
+
pawls_pages: PawlsPagePythonType data with original PDF dimension/tokens.
|
|
121
|
+
page_redactions: BBoxes to redact (parallel to pawls_pages).
|
|
122
|
+
dpi: The DPI used when converting PDF to images.
|
|
123
|
+
hide_text: If True, text is 100% invisible (transparent).
|
|
124
|
+
"""
|
|
125
|
+
c = canvas.Canvas(output_pdf)
|
|
126
|
+
|
|
127
|
+
# Predefine a fully transparent color for text
|
|
128
|
+
invisible_color = Color(red=0, green=0, blue=0, alpha=0.0)
|
|
129
|
+
|
|
130
|
+
for page_idx, img in enumerate(redacted_images):
|
|
131
|
+
pawls_page = pawls_pages[page_idx]
|
|
132
|
+
redactions = page_redactions[page_idx]
|
|
133
|
+
|
|
134
|
+
width_px, height_px = img.size
|
|
135
|
+
|
|
136
|
+
# Convert pixel dimensions to PDF points
|
|
137
|
+
width_pts = (width_px * 72.0) / dpi
|
|
138
|
+
height_pts = (height_px * 72.0) / dpi
|
|
139
|
+
|
|
140
|
+
c.setPageSize((width_pts, height_pts))
|
|
141
|
+
|
|
142
|
+
# Draw the redacted image onto the canvas
|
|
143
|
+
buf = io.BytesIO()
|
|
144
|
+
img.save(buf, format="PNG")
|
|
145
|
+
buf.seek(0)
|
|
146
|
+
img_reader = ImageReader(buf)
|
|
147
|
+
c.drawImage(img_reader, 0, 0, width=width_pts, height=height_pts)
|
|
148
|
+
|
|
149
|
+
# If hide_text is True, paint text with alpha=0.0 for transparency
|
|
150
|
+
if hide_text:
|
|
151
|
+
c.setFillColor(invisible_color)
|
|
152
|
+
else:
|
|
153
|
+
# Visible text in black
|
|
154
|
+
c.setFillColorRGB(0, 0, 0)
|
|
155
|
+
|
|
156
|
+
page_w = float(pawls_page["page"]["width"])
|
|
157
|
+
page_h = float(pawls_page["page"]["height"])
|
|
158
|
+
tokens = pawls_page["tokens"]
|
|
159
|
+
|
|
160
|
+
for token in tokens:
|
|
161
|
+
# Skip tokens that intersect redactions
|
|
162
|
+
if _is_token_in_redactions(token, redactions):
|
|
163
|
+
continue
|
|
164
|
+
|
|
165
|
+
x_left = float(token["x"])
|
|
166
|
+
y_top = float(token["y"])
|
|
167
|
+
token_h = float(token["height"])
|
|
168
|
+
|
|
169
|
+
# Convert top-based coords to bottom-based
|
|
170
|
+
x_pt = (x_left / page_w) * width_pts
|
|
171
|
+
y_pt = height_pts - ((y_top + token_h) / page_h) * height_pts
|
|
172
|
+
|
|
173
|
+
# Add a space so the tokens remain separated on copy/paste
|
|
174
|
+
c.drawString(x_pt, y_pt, token["text"] + " ")
|
|
175
|
+
|
|
176
|
+
c.showPage()
|
|
177
|
+
|
|
178
|
+
c.save()
|
|
179
|
+
logger.info(f"Created {output_pdf} with fully transparent text.")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _is_token_in_redactions(
|
|
183
|
+
token_dict: dict[str, float | str], redactions: list[OpenContractsSinglePageAnnotationType]
|
|
184
|
+
) -> bool:
|
|
185
|
+
"""
|
|
186
|
+
Checks if a token's bounding box intersects any redaction bounding box.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
token_dict: A single token with x,y,width,height in PDF coords.
|
|
190
|
+
redactions: A list of annotation bboxes in PDF coords.
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
True if the token overlaps at least one redaction region, False otherwise.
|
|
194
|
+
"""
|
|
195
|
+
x_left = float(token_dict["x"])
|
|
196
|
+
y_top = float(token_dict["y"])
|
|
197
|
+
w = float(token_dict["width"])
|
|
198
|
+
h = float(token_dict["height"])
|
|
199
|
+
|
|
200
|
+
token_r = x_left + w
|
|
201
|
+
token_b = y_top + h
|
|
202
|
+
|
|
203
|
+
for annotation in redactions:
|
|
204
|
+
bbox = annotation["bounds"]
|
|
205
|
+
b_left = float(bbox["left"])
|
|
206
|
+
b_right = float(bbox["right"])
|
|
207
|
+
b_top = float(bbox["top"])
|
|
208
|
+
b_bottom = float(bbox["bottom"])
|
|
209
|
+
|
|
210
|
+
# Check for overlap (token vs. bbox)
|
|
211
|
+
if not (
|
|
212
|
+
token_r < b_left # entirely left
|
|
213
|
+
or x_left > b_right # entirely right
|
|
214
|
+
or token_b < b_top # entirely above
|
|
215
|
+
or y_top > b_bottom # entirely below
|
|
216
|
+
):
|
|
217
|
+
return True
|
|
218
|
+
|
|
219
|
+
return False
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pdfredact
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Project-URL: Documentation, https://github.com/JSv4/PdfRedact#readme
|
|
5
|
+
Project-URL: Issues, https://github.com/JSv4/PdfRedact/issues
|
|
6
|
+
Project-URL: Source, https://github.com/JSv4/PdfRedact
|
|
7
|
+
Author-email: JSv4 <scrudato@umich.edu>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE.txt
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Requires-Dist: pdf2image==1.17.0
|
|
21
|
+
Requires-Dist: pillow==11.1.0
|
|
22
|
+
Requires-Dist: plasmapdf==0.1.1
|
|
23
|
+
Requires-Dist: rapidfuzz==3.11.0
|
|
24
|
+
Requires-Dist: reportlab==4.2.5
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# PdfRedact
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/pdfredact)
|
|
30
|
+
[](https://pypi.org/project/pdfredact)
|
|
31
|
+
|
|
32
|
+
-----
|
|
33
|
+
|
|
34
|
+
## Table of Contents
|
|
35
|
+
|
|
36
|
+
- [Installation](#installation)
|
|
37
|
+
- [License](#license)
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```console
|
|
42
|
+
pip install pdfredact
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
`pdfredact` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
pdfredact/__about__.py,sha256=WuWMCo9v9W-Zqbuy7RLS_tZI7FbuTZH-H462uWLXa9g,124
|
|
2
|
+
pdfredact/__init__.py,sha256=7zwEYRzFlqYVpSKQIzb4k9fDZfpX-uxp5fFupuuXCrs,262
|
|
3
|
+
pdfredact/utils.py,sha256=M6_IvoFCak5hmClULMcodwlrXkFZsRDqypLwkr_16GY,7650
|
|
4
|
+
pdfredact-0.0.1.dist-info/METADATA,sha256=L7odryYe99LSxJiuta3RQsqfqYWKXDGavPhhEysqglM,1517
|
|
5
|
+
pdfredact-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
+
pdfredact-0.0.1.dist-info/licenses/LICENSE.txt,sha256=wvIPgHGvCpGc3uXMr3bDMY0y90-tewONiM3wYNznULc,1099
|
|
7
|
+
pdfredact-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present JSv4 <scrudato@umich.edu>
|
|
4
|
+
|
|
5
|
+
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:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
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.
|