from pathlib import Path
from pypdf import PdfReader, PdfWriter, Transformation, PageObject

INPUT_PDF = Path(r"2009-05-04 Puzzle SudokuKiller 376 [difficulty 4 of 10] [average solving time 30 min].pdf")
OUTPUT_PDF = Path(r"sudoku_50pct_A4.pdf")

# A4 v bodech, 72 dpi
A4_WIDTH = 595.2756
A4_HEIGHT = 841.8898

SCALE = 0.5

reader = PdfReader(str(INPUT_PDF))
source_page = reader.pages[0]

source_width = float(source_page.mediabox.width)
source_height = float(source_page.mediabox.height)

# Nová prázdná A4 stránka
new_page = PageObject.create_blank_page(
    width=A4_WIDTH,
    height=A4_HEIGHT
)

# Výpočet pozice pro vycentrování
target_width = source_width * SCALE
target_height = source_height * SCALE

x = (A4_WIDTH - target_width) / 2
y = (A4_HEIGHT - target_height) / 2

# Vložit původní PDF stránku jako vektorový objekt, zmenšený na 50 %
transform = Transformation().scale(SCALE).translate(x, y)
new_page.merge_transformed_page(source_page, transform, expand=False)

writer = PdfWriter()
writer.add_page(new_page)

with OUTPUT_PDF.open("wb") as f:
    writer.write(f)

print(f"Hotovo: {OUTPUT_PDF}")