Skip to content

How to set different margins for landscape and portrait layouts? #70

Answered by JorjMcKie
bodzebod asked this question in Q&A
Discussion options

You must be logged in to vote

You need to invoke method to_markdown page-wise, i.e. use the parameter pages=[page.number]. Then you can give each page some special treatment and / or adjust invocation parameters.
The one thing to take care of is to execute Section Header Identifications only once and provide that result to each invocation.
Here is a snippet.

import pymupdf, pymupdf4llm

doc = pymupdf.open("input.pdf")
hdr_info = pymupdf4llm.IdentifyHeaders(doc)  # perform a one-time scan of font sizes
mdtext = ""
for page in doc:
    rect = page.rect  # the page rectangle
    if rect.width > rect.height:  # do some computations for margins
        margins = (...)  # landscape values
    else:
        margins = (...)  #…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@bodzebod
Comment options

@JorjMcKie
Comment options

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