Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New Features (ENH)
- New `extract-annotated-pages` to filter out only the user annotated pages ([PR #98](https://github.com/py-pdf/pdfly/pull/98))
- Added optional `--password` argument to `cat` to perform decryption ([PR #61](https://github.com/py-pdf/pdfly/pull/61))
- `pagemeta` now display known page formats when it can detect it: A3, A4, A5, Letter, Legal

### Bug Fixes (BUG)
- `pypdf[full]` is now a dependency, instead of just `pypdf`, to avoid some cases of `DependencyError`
Expand Down
55 changes: 28 additions & 27 deletions pdfly/pagemeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@

from pydantic import BaseModel
from pypdf import PdfReader
from rich.console import Console
from rich.markdown import Markdown
from rich.table import Table

from ._utils import OutputOptions

KNOWN_PAGE_FORMATS = {
(841.89, 1190.55): "A3",
(595.28, 841.89): "A4",
(420.94, 595.28): "A5",
(612, 792): "Letter",
(612, 1008): "Legal",
}


class PageMeta(BaseModel):
mediabox: Tuple[float, float, float, float]
Expand All @@ -31,40 +42,30 @@ def main(pdf: Path, page_index: int, output: OutputOptions) -> None:
if output == OutputOptions.json:
print(meta.json())
else:
from rich.console import Console
from rich.markdown import Markdown
from rich.table import Table

console = Console()

table = Table(title=f"{pdf}, page index {page_index}")
table.add_column(
"Attribute", justify="right", style="cyan", no_wrap=True
)
table.add_column("Value", style="white")

table.add_row(
"mediabox",
f"{meta.mediabox}: "
f"with={meta.mediabox[2] - meta.mediabox[0]} "
f"x height={meta.mediabox[3] - meta.mediabox[1]}",
)
table.add_row(
"cropbox",
f"{meta.cropbox}: "
f"with={meta.cropbox[2] - meta.cropbox[0]} "
f"x height={meta.cropbox[3] - meta.cropbox[1]}",
)
table.add_row(
"artbox",
f"{meta.artbox}: with={meta.artbox[2] - meta.artbox[0]} x height={meta.artbox[3] - meta.artbox[1]}",
)
table.add_row(
"bleedbox",
f"{meta.bleedbox}: "
f"with={meta.bleedbox[2] - meta.bleedbox[0]} "
f"x height={meta.bleedbox[3] - meta.bleedbox[1]}",
)
def add_box_attr(
name: str, box: Tuple[float, float, float, float]
) -> None:
width = box[2] - box[0]
height = box[3] - box[1]
known_format = KNOWN_PAGE_FORMATS.get((width, height))
extra = f" ({known_format})" if known_format else ""
table.add_row(
name,
f"({box[0]:.2f}, {box[1]:.2f}, {box[2]:.2f}, {box[3]:.2f}):"
f" {width=:.2f} x {height=:.2f}{extra}",
)

add_box_attr("mediabox", meta.mediabox)
add_box_attr("cropbox", meta.cropbox)
add_box_attr("artbox", meta.artbox)
add_box_attr("bleedbox", meta.bleedbox)

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

Expand Down
Loading