55
66from pydantic import BaseModel
77from pypdf import PdfReader
8+ from pypdf .generic import RectangleObject
9+ from rich .console import Console
10+ from rich .markdown import Markdown
11+ from rich .table import Table
812
913from ._utils import OutputOptions
1014
15+ KNOWN_PAGE_FORMATS = {
16+ (841.89 , 1190.55 ): "A3" ,
17+ (595.28 , 841.89 ): "A4" ,
18+ (420.94 , 595.28 ): "A5" ,
19+ (612 , 792 ): "Letter" ,
20+ (612 , 1008 ): "Legal" ,
21+ }
22+
1123
1224class PageMeta (BaseModel ):
1325 mediabox : Tuple [float , float , float , float ]
@@ -31,40 +43,28 @@ def main(pdf: Path, page_index: int, output: OutputOptions) -> None:
3143 if output == OutputOptions .json :
3244 print (meta .json ())
3345 else :
34- from rich .console import Console
35- from rich .markdown import Markdown
36- from rich .table import Table
37-
3846 console = Console ()
39-
4047 table = Table (title = f"{ pdf } , page index { page_index } " )
4148 table .add_column (
4249 "Attribute" , justify = "right" , style = "cyan" , no_wrap = True
4350 )
4451 table .add_column ("Value" , style = "white" )
4552
46- table .add_row (
47- "mediabox" ,
48- f"{ meta .mediabox } : "
49- f"with={ meta .mediabox [2 ] - meta .mediabox [0 ]} "
50- f"x height={ meta .mediabox [3 ] - meta .mediabox [1 ]} " ,
51- )
52- table .add_row (
53- "cropbox" ,
54- f"{ meta .cropbox } : "
55- f"with={ meta .cropbox [2 ] - meta .cropbox [0 ]} "
56- f"x height={ meta .cropbox [3 ] - meta .cropbox [1 ]} " ,
57- )
58- table .add_row (
59- "artbox" ,
60- f"{ meta .artbox } : with={ meta .artbox [2 ] - meta .artbox [0 ]} x height={ meta .artbox [3 ] - meta .artbox [1 ]} " ,
61- )
62- table .add_row (
63- "bleedbox" ,
64- f"{ meta .bleedbox } : "
65- f"with={ meta .bleedbox [2 ] - meta .bleedbox [0 ]} "
66- f"x height={ meta .bleedbox [3 ] - meta .bleedbox [1 ]} " ,
67- )
53+ def add_box_attr (name : str , box : RectangleObject ) -> None :
54+ width = box [2 ] - box [0 ]
55+ height = box [3 ] - box [1 ]
56+ known_format = KNOWN_PAGE_FORMATS .get ((width , height ))
57+ extra = f" ({ known_format } )" if known_format else ""
58+ table .add_row (
59+ name ,
60+ f"({ box [0 ]:.2f} , { box [1 ]:.2f} , { box [2 ]:.2f} , { box [3 ]:.2f} ):"
61+ f" { width = :.2f} x { height = :.2f} { extra } " ,
62+ )
63+
64+ add_box_attr ("mediabox" , meta .mediabox )
65+ add_box_attr ("cropbox" , meta .cropbox )
66+ add_box_attr ("artbox" , meta .artbox )
67+ add_box_attr ("bleedbox" , meta .bleedbox )
6868
6969 table .add_row ("annotations" , str (meta .annotations ))
7070
0 commit comments