File size: 1,405 Bytes
ffe74d2
 
 
 
27198d6
3f40d81
27198d6
ffe74d2
e0b8c35
ffe74d2
 
 
3f40d81
ffe74d2
 
 
 
 
 
e0b8c35
ffe74d2
 
 
 
 
 
e0b8c35
 
 
 
 
ffe74d2
 
e0b8c35
 
ffe74d2
 
 
 
 
e0b8c35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()