"""Moteur Masquage (Masking Tape) — rectangles blancs.""" from pypdf import PdfReader, PdfWriter from reportlab.pdfgen import canvas from .pdf_utils import get_page_size import io def apply_masking(input_path, output_path, rectangles, apply_to="all"): """Appliquer des rectangles blancs de masquage. rectangles: liste de dicts {"x": pt, "y": pt, "width": pt, "height": pt} apply_to: "all", "even", "odd", ou un numero de page (1-based) """ reader = PdfReader(input_path) writer = PdfWriter() for i, page in enumerate(reader.pages): page_num = i + 1 should_apply = False if apply_to == "all": should_apply = True elif apply_to == "even": should_apply = page_num % 2 == 0 elif apply_to == "odd": should_apply = page_num % 2 != 0 elif isinstance(apply_to, int): should_apply = page_num == apply_to if should_apply and rectangles: w, h = get_page_size(page) packet = io.BytesIO() c = canvas.Canvas(packet, pagesize=(w, h)) c.setFillColorRGB(1, 1, 1) c.setStrokeColorRGB(1, 1, 1) for rect in rectangles: c.rect(rect["x"], rect["y"], rect["width"], rect["height"], fill=1, stroke=0) c.showPage() c.save() packet.seek(0) overlay_reader = PdfReader(packet) page.merge_page(overlay_reader.pages[0]) writer.add_page(page) with open(output_path, "wb") as f: writer.write(f) return output_path