"""
Render one or more pages of a PDF to PNG files.

Usage:
  python render_pdf_page.py <pdf_path> <page_numbers> [--dpi 300] [--out-dir ./pages]
  python render_pdf_page.py "F:/ЦТ/2018.pdf" "5,6,7,8" --dpi 300 --out-dir ./tmp_pages

page_numbers: comma-separated 1-based page numbers
"""
import sys
import os
import argparse
import fitz  # PyMuPDF

def render_pages(pdf_path, pages, dpi=300, out_dir='.'):
    os.makedirs(out_dir, exist_ok=True)
    doc = fitz.open(pdf_path)
    scale = dpi / 72.0
    mat = fitz.Matrix(scale, scale)
    results = []
    for page_num in pages:
        idx = page_num - 1
        if idx < 0 or idx >= len(doc):
            print(f'  Page {page_num} out of range (doc has {len(doc)} pages)', file=sys.stderr)
            continue
        page = doc[idx]
        pix = page.get_pixmap(matrix=mat)
        out_path = os.path.join(out_dir, f'page_{page_num:03d}.png')
        pix.save(out_path)
        results.append(out_path)
        print(f'  Rendered page {page_num} -> {out_path} ({pix.width}x{pix.height})')
    doc.close()
    return results

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('pdf_path')
    parser.add_argument('pages', help='comma-separated 1-based page numbers, e.g. "5,6,7,8"')
    parser.add_argument('--dpi', type=int, default=300)
    parser.add_argument('--out-dir', default='./tmp_pages')
    args = parser.parse_args()
    pages = [int(p.strip()) for p in args.pages.split(',')]
    render_pages(args.pdf_path, pages, dpi=args.dpi, out_dir=args.out_dir)
