import gradio as gr import pikepdf from io import BytesIO def remove_pdf_password(file, output_file_name, password_prefix, start_year, end_year): output_path = f"/content/{output_file_name}" for year in range(start_year, end_year + 1): password = f"{password_prefix}{year}" try: with pikepdf.open(BytesIO(file), password=password) as pdf: pdf.save(output_path) return output_path, f"Password '{password}' removed successfully." except pikepdf.PasswordError: continue except Exception as e: return None, f"Failed to remove password with '{password}': {e}" return None, "Failed to remove password with any of the expected passwords." # Gradio interface interface = gr.Interface( fn=remove_pdf_password, inputs=[ gr.File(label="Upload PDF", type="filepath"), gr.Textbox(label="Output File Name"), gr.Textbox(label="Password Prefix"), gr.Slider(1900, 2100, step=1, label="Start Year"), gr.Slider(1900, 2100, step=1, label="End Year"), ], outputs=[ gr.File(label="Decrypted PDF"), gr.Textbox(label="Status") ], title="PDF Password Remover", description="Upload a password-protected PDF, provide the password prefix and the range of years to try, and get the decrypted PDF file." ) interface.launch()