Skip to content

Commit d927769

Browse files
committed
ENH: pagemeta now displays known page formats when it can detect it
1 parent 6e5a4ae commit d927769

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features (ENH)
66
- New `extract-annotated-pages` to filter out only the user annotated pages ([PR #98](https://github.com/py-pdf/pdfly/pull/98))
77
- Added optional `--password` argument to `cat` to perform decryption ([PR #61](https://github.com/py-pdf/pdfly/pull/61))
8+
- `pagemeta` now display known page formats when it can detect it: A3, A4, A5, Letter, Legal
89

910
### Bug Fixes (BUG)
1011
- `pypdf[full]` is now a dependency, instead of just `pypdf`, to avoid some cases of `DependencyError`

pdfly/pagemeta.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55

66
from pydantic import BaseModel
77
from pypdf import PdfReader
8+
from rich.console import Console
9+
from rich.markdown import Markdown
10+
from rich.table import Table
811

912
from ._utils import OutputOptions
1013

14+
KNOWN_PAGE_FORMATS = {
15+
(841.89, 1190.55): "A3",
16+
(595.28, 841.89): "A4",
17+
(420.94, 595.28): "A5",
18+
(612, 792): "Letter",
19+
(612, 1008): "Legal",
20+
}
21+
1122

1223
class PageMeta(BaseModel):
1324
mediabox: Tuple[float, float, float, float]
@@ -31,40 +42,30 @@ def main(pdf: Path, page_index: int, output: OutputOptions) -> None:
3142
if output == OutputOptions.json:
3243
print(meta.json())
3344
else:
34-
from rich.console import Console
35-
from rich.markdown import Markdown
36-
from rich.table import Table
37-
3845
console = Console()
39-
4046
table = Table(title=f"{pdf}, page index {page_index}")
4147
table.add_column(
4248
"Attribute", justify="right", style="cyan", no_wrap=True
4349
)
4450
table.add_column("Value", style="white")
4551

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-
)
52+
def add_box_attr(
53+
name: str, box: Tuple[float, float, float, float]
54+
) -> None:
55+
width = box[2] - box[0]
56+
height = box[3] - box[1]
57+
known_format = KNOWN_PAGE_FORMATS.get((width, height))
58+
extra = f" ({known_format})" if known_format else ""
59+
table.add_row(
60+
name,
61+
f"({box[0]:.2f}, {box[1]:.2f}, {box[2]:.2f}, {box[3]:.2f}):"
62+
f" {width=:.2f} x {height=:.2f}{extra}",
63+
)
64+
65+
add_box_attr("mediabox", meta.mediabox)
66+
add_box_attr("cropbox", meta.cropbox)
67+
add_box_attr("artbox", meta.artbox)
68+
add_box_attr("bleedbox", meta.bleedbox)
6869

6970
table.add_row("annotations", str(meta.annotations))
7071

0 commit comments

Comments
 (0)