-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_optimize.py
More file actions
57 lines (45 loc) · 1.54 KB
/
Copy path09_optimize.py
File metadata and controls
57 lines (45 loc) · 1.54 KB
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
"""
09 - Optimize (compress and reduce file size).
- Default profile: stream compression + dedupe
- Aggressive: also downsample images and strip metadata
Typical reductions: 40-70% on image-heavy PDFs.
Run:
python examples/09_optimize.py
"""
from _common import (
header, info, success, ensure_output_dir, get_sample_pdf, init_license,
)
import exis_pdfeditor
def main() -> None:
init_license()
header("Demo 09 - Optimize")
sample = get_sample_pdf()
out = ensure_output_dir()
# Default optimization
info("Default optimization (compress + dedupe)...")
result = exis_pdfeditor.optimize(
str(sample),
str(out / "09-default.pdf"),
)
success(
f"Reduced {result.originalSize:,} -> {result.optimizedSize:,} bytes "
f"({result.reductionPercent:.1f}% smaller)"
)
print(f" Streams compressed: {result.streamsCompressed}")
print(f" Duplicates removed: {result.duplicatesRemoved}")
# Aggressive: downsample images, strip metadata
info("Aggressive optimization (downsample images, strip metadata)...")
aggressive = exis_pdfeditor.optimize(
str(sample),
str(out / "09-aggressive.pdf"),
downsample_images=True,
max_image_dpi=150,
remove_metadata=True,
)
success(
f"Reduced {aggressive.originalSize:,} -> {aggressive.optimizedSize:,} bytes "
f"({aggressive.reductionPercent:.1f}% smaller)"
)
print(f" Images downsampled: {aggressive.imagesDownsampled}")
if __name__ == "__main__":
main()