import fitz from pathlib import Path # ========================================= # CONFIG # ========================================= input_path = Path(r"u:\Dropbox\Ordinace\Dokumentace_ke_zpracování\A4 expand\mericka_rotated_inplace.pdf") output_path = input_path.with_name(input_path.stem + "_A4.pdf") A4_WIDTH = 595.28 A4_HEIGHT = 841.89 def place_on_a4(input_pdf: Path, output_pdf: Path): src = fitz.open(input_pdf) dst = fitz.open() for page in src: # Extract actual width & height of rotated page w = page.rect.width h = page.rect.height # --- SCALE to A4 width --- scale_factor = A4_WIDTH / w new_w = A4_WIDTH new_h = h * scale_factor # Create A4 page new_page = dst.new_page(width=A4_WIDTH, height=A4_HEIGHT) # Center vertically x = 0 y = (A4_HEIGHT - new_h) / 2 target_rect = fitz.Rect(x, y, x + new_w, y + new_h) # Draw original PDF onto A4 page new_page.show_pdf_page(target_rect, src, page.number) dst.save(output_pdf) dst.close() src.close() print("Saved A4 adapted:", output_pdf) place_on_a4(input_path, output_path)