Skip to content

PDF margins crop #1023

Apr 20, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

That's not easy, because there is no official PDF property to read this out.
You only can compute the maximum (union) rectangle for things on the page.
This computes the union of all text and images:

rect = fitz.Rect()  # start empty
for b in page.get_text("blocks"):
    rect |= b[:4]  # join block rectangle into the total

If you want to be overly diligent, also look for any drawings (these are not images!):

for path in page.get_drawings():
    rect |= path["rect"]

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pajakpawel
Comment options

Answer selected by pajakpawel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants