File tree 3 files changed +37
-4
lines changed
3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,11 @@ Options:
30
30
Commands:
31
31
2-up Create a booklet-style PDF from a single input.
32
32
cat Concatenate pages from PDF files into a single PDF file.
33
+ compress Compress a PDF.
33
34
extract-images Extract images from PDF without resampling or altering.
35
+ extract-text Extract text from a PDF file.
36
+ meta Show metadata of a PDF file
37
+
34
38
```
35
39
36
40
You can see the help of every subcommand by typing:
Original file line number Diff line number Diff line change 4
4
import typer
5
5
6
6
import cpdf .cat
7
+ import cpdf .compress
7
8
import cpdf .extract_images
8
9
import cpdf .metadata
9
10
import cpdf .up2
@@ -57,7 +58,7 @@ def cat(
57
58
def metadata (
58
59
pdf : Path ,
59
60
output : cpdf .metadata .OutputOptions = typer .Option ( # noqa
60
- cpdf .metadata .OutputOptions .text ,
61
+ cpdf .metadata .OutputOptions .text . value ,
61
62
"--output" ,
62
63
"-o" ,
63
64
help = "output format" ,
@@ -68,9 +69,7 @@ def metadata(
68
69
69
70
70
71
@entry_point .command (name = "extract-text" ) # type: ignore[misc]
71
- def extract_text (
72
- pdf : Path ,
73
- ):
72
+ def extract_text (pdf : Path ):
74
73
"""Extract text from a PDF file."""
75
74
from PyPDF2 import PdfReader
76
75
@@ -79,7 +78,13 @@ def extract_text(
79
78
print (page .extract_text ())
80
79
81
80
81
+ @entry_point .command (name = "compress" ) # type: ignore[misc]
82
+ def compress (pdf : Path , output : Path ):
83
+ cpdf .compress .main (pdf , output )
84
+
85
+
82
86
up2 .__doc__ = cpdf .up2 .__doc__
83
87
extract_images .__doc__ = cpdf .extract_images .__doc__
84
88
cat .__doc__ = cpdf .cat .__doc__
85
89
metadata .__doc__ = cpdf .metadata .__doc__
90
+ compress .__doc__ = cpdf .compress .__doc__
Original file line number Diff line number Diff line change
1
+ """Compress a PDF."""
2
+
3
+ import os
4
+ from pathlib import Path
5
+
6
+ from PyPDF2 import PdfReader , PdfWriter
7
+
8
+
9
+ def main (pdf : Path , output : Path ):
10
+ reader = PdfReader (pdf )
11
+ writer = PdfWriter ()
12
+ for page in reader .pages :
13
+ page .compress_content_streams ()
14
+ writer .add_page (page )
15
+
16
+ with open (output , "wb" ) as fp :
17
+ writer .write (fp )
18
+
19
+ orig_size = os .path .getsize (pdf )
20
+ comp_size = os .path .getsize (output )
21
+ ratio = comp_size / orig_size
22
+
23
+ print (f"Original Size : { orig_size :,} " )
24
+ print (f"Compressed Size: { comp_size :,} ({ ratio * 100 :2.1f} % of original)" )
You can’t perform that action at this time.
0 commit comments