Spaces:
Sleeping
Sleeping
File size: 1,284 Bytes
e6ea8c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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)
|