File size: 1,424 Bytes
ca8d0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import fitz
import os


def append_suffix_to_filename(filepath, suffix):
    directory, filename = os.path.split(filepath)
    filename_without_extension, extension = os.path.splitext(filename)
    return os.path.join(directory, filename_without_extension + suffix + extension)


def remove_password(input_files, password):
    output_files = []
    for file in input_files:
        output_path = append_suffix_to_filename(file.name, '_open')
        try:
            doc = fitz.open(file.name)
            if doc.is_encrypted:
                doc.authenticate(password)
            doc.save(output_path)
            print(output_path)
            doc.close()
            output_files.append(output_path)
        except Exception as e:
            print(e)
            pass
    return output_files


def pdf_password_remover(files, password):
    output_files = []
    for file in files:
        output_files.extend(remove_password([file], password))
    return output_files


input_components = [
    gr.Files(label="Upload PDF(s)"),
    gr.Textbox(label="Password", type="text")
]

output_component = [gr.Files(label="Download Unlocked PDF(s)")]

gr.Interface(
    fn=pdf_password_remover,
    inputs=input_components,
    outputs=output_component,
    title="PDF Password Remover",
    description="Upload password-protected PDF files and input a single password to unlock them."
).launch(debug=True)