pdfinspector / debug_rotation.py
rianders's picture
Include example PDFs with Git LFS and add helper scripts
e6ea8c6
import pymupdf as fitz
from PIL import Image, ImageDraw
def create_rotated_pdf():
doc = fitz.open()
page = doc.new_page()
# Insert text at specific coordinates (100, 100) -> (200, 150) roughly
page.insert_text((100, 100), "Hello World", fontsize=20)
# Rotate page 90 degrees
page.set_rotation(90)
doc.save("rotated_test.pdf")
return "rotated_test.pdf"
def analyze_pdf(path):
doc = fitz.open(path)
page = doc[0]
print(f"Page Rotation: {page.rotation}")
print(f"Page Rect (unrotated): {page.rect}")
# Pixmap
pix = page.get_pixmap()
print(f"Pixmap WxH: {pix.width}x{pix.height}")
# Text Dict
raw = page.get_text("dict")
block = raw["blocks"][0]
bbox = fitz.Rect(block["bbox"])
print(f"Text Bbox (raw): {bbox}")
# Try transforming
# page.rotation_matrix might not exist as a property, let's check dir
if hasattr(page, "rotation_matrix"):
mat = page.rotation_matrix
print(f"Page.rotation_matrix: {mat}")
t_bbox = bbox * mat
print(f"Transformed Bbox: {t_bbox}")
else:
print("No page.rotation_matrix")
doc.close()
if __name__ == "__main__":
pdf_path = create_rotated_pdf()
analyze_pdf(pdf_path)