import fitz  # PyMuPDF

def compress_pdf(input_pdf, output_pdf, quality=50):
    """Compresses a PDF by reducing image quality."""
    doc = fitz.open(input_pdf)
    
    for page in doc:
        for img in page.get_images(full=True):
            xref = img[0]
            pix = fitz.Pixmap(doc, xref)
            
            if pix.n > 3:  # Convert CMYK to RGB
                pix = fitz.Pixmap(fitz.csRGB, pix)
                
            pix.save(output_pdf, quality=quality)
    
    doc.save(output_pdf)
    doc.close()
    print(f"Compressed PDF saved at {output_pdf}")
