22
33import stat
44from datetime import datetime
5- from enum import Enum
65from pathlib import Path
7- from typing import Optional , Set , Tuple
6+ from typing import Optional , Set
87
98from pydantic import BaseModel
109from pypdf import PdfReader
1110
11+ from ._utils import OutputOptions
12+
1213
1314class MetaInfo (BaseModel ):
1415 title : Optional [str ] = None
1516 producer : Optional [str ] = None
1617 author : Optional [str ] = None
1718 pages : int
1819 encrypted : bool
19- page_size : Tuple [float , float ] # (width, height)
2020 pdf_file_version : str
2121 page_mode : Optional [str ]
2222 page_layout : Optional [str ]
@@ -29,23 +29,16 @@ class MetaInfo(BaseModel):
2929 access_time : datetime
3030
3131
32- class OutputOptions (Enum ):
33- json = "json"
34- text = "text"
35-
36-
3732def main (pdf : Path , output : OutputOptions ) -> None :
3833 reader = PdfReader (str (pdf ))
3934 info = reader .metadata
40- x1 , y1 , x2 , y2 = reader .pages [0 ].mediabox
4135
4236 reader .stream .seek (0 )
4337 pdf_file_version = reader .stream .read (8 ).decode ("utf-8" )
4438 pdf_stat = pdf .stat ()
4539 meta = MetaInfo (
4640 pages = len (reader .pages ),
4741 encrypted = reader .is_encrypted ,
48- page_size = (x2 - x1 , y2 - y1 ),
4942 page_mode = reader .page_mode ,
5043 pdf_file_version = pdf_file_version ,
5144 page_layout = reader .page_layout ,
@@ -68,17 +61,16 @@ def main(pdf: Path, output: OutputOptions) -> None:
6861 from rich .table import Table
6962
7063 table = Table (title = "PDF Data" )
71- table .add_column ("Attribute" , justify = "right" , style = "cyan" , no_wrap = True )
64+ table .add_column (
65+ "Attribute" , justify = "right" , style = "cyan" , no_wrap = True
66+ )
7267 table .add_column ("Value" , style = "white" )
7368
7469 table .add_row ("Title" , meta .title )
7570 table .add_row ("Producer" , meta .producer )
7671 table .add_row ("Author" , meta .author )
7772 table .add_row ("Pages" , f"{ meta .pages :,} " )
7873 table .add_row ("Encrypted" , f"{ meta .encrypted } " )
79- table .add_row (
80- "Page size" , f"{ meta .page_size [0 ]} x { meta .page_size [1 ]} pts (w x h)"
81- )
8274 table .add_row ("PDF File Version" , meta .pdf_file_version )
8375 table .add_row ("Page Layout" , meta .page_layout )
8476 table .add_row ("Page Mode" , meta .page_mode )
@@ -92,17 +84,26 @@ def main(pdf: Path, output: OutputOptions) -> None:
9284 table .add_row ("Fonts (embedded)" , ", " .join (sorted (embedded_fonts )))
9385
9486 os_table = Table (title = "Operating System Data" )
95- os_table .add_column ("Attribute" , justify = "right" , style = "cyan" , no_wrap = True )
87+ os_table .add_column (
88+ "Attribute" , justify = "right" , style = "cyan" , no_wrap = True
89+ )
9690 os_table .add_column ("Value" , style = "white" )
9791 os_table .add_row ("File Name" , f"{ pdf } " )
9892 os_table .add_row ("File Permissions" , f"{ meta .file_permissions } " )
9993 os_table .add_row ("File Size" , f"{ meta .file_size :,} bytes" )
100- os_table .add_row ("Creation Time" , f"{ meta .creation_time :%Y-%m-%d %H:%M:%S} " )
94+ os_table .add_row (
95+ "Creation Time" , f"{ meta .creation_time :%Y-%m-%d %H:%M:%S} "
96+ )
10197 os_table .add_row (
10298 "Modification Time" , f"{ meta .modification_time :%Y-%m-%d %H:%M:%S} "
10399 )
104- os_table .add_row ("Access Time" , f"{ meta .access_time :%Y-%m-%d %H:%M:%S} " )
100+ os_table .add_row (
101+ "Access Time" , f"{ meta .access_time :%Y-%m-%d %H:%M:%S} "
102+ )
105103
106104 console = Console ()
107105 console .print (os_table )
108106 console .print (table )
107+ console .print (
108+ "Use the 'pagemeta' subcommand to get details about a single page"
109+ )
0 commit comments