File size: 3,791 Bytes
4c85f97
 
 
 
 
f13099a
21bc43d
4c85f97
481213a
72673b1
4c85f97
 
72673b1
4c85f97
 
39ae163
 
4c85f97
 
 
39ae163
 
 
 
 
 
 
 
 
 
 
 
 
21bc43d
4c85f97
 
 
fdc1d79
21bc43d
fdc1d79
21bc43d
fdc1d79
21bc43d
fdc1d79
87d950a
fdc1d79
 
 
4c85f97
21bc43d
 
 
 
4c85f97
 
 
 
21bc43d
 
87d950a
 
21bc43d
87d950a
 
 
 
21bc43d
 
4c85f97
 
 
39ae163
 
481213a
f13099a
 
481213a
4c85f97
e0b9bcc
4c85f97
e0b9bcc
4c85f97
 
 
 
 
 
fdc1d79
4c85f97
e0b9bcc
4c85f97
 
 
 
481213a
4c85f97
 
 
 
e0b9bcc
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import PyPDF2
import requests
import io
import tempfile
import sys
import os

def compress_pdf(input_file, url, strength):
    if input_file is None and (url is None or url.strip() == ""):
        return None, "Please provide either a file or a URL."
    
    if input_file is not None and url and url.strip() != "":
        return None, "Please provide either a file or a URL, not both."
    
    try:
        if url and url.strip() != "":
            response = requests.get(url)
            response.raise_for_status()
            pdf_content = io.BytesIO(response.content)
            initial_size = len(response.content)
        else:
            if hasattr(input_file, 'name'):
                # If input_file is a file path
                with open(input_file.name, 'rb') as file:
                    pdf_content = io.BytesIO(file.read())
                initial_size = os.path.getsize(input_file.name)
            else:
                # If input_file is file-like object
                pdf_content = io.BytesIO(input_file.read())
                pdf_content.seek(0, io.SEEK_END)
                initial_size = pdf_content.tell()
                pdf_content.seek(0)

        reader = PyPDF2.PdfReader(pdf_content)
        writer = PyPDF2.PdfWriter()

        if strength == "Low":
            target_ratio = 0.75  # 25% compression
        elif strength == "Medium":
            target_ratio = 0.50  # 50% compression
        else:  # High
            target_ratio = 0.25  # 75% compression

        # Apply compression to each page
        for page in reader.pages:
            page.compress_content_streams()  # Apply content stream compression
            writer.add_page(page)

        # Set compression parameters
        writer.compress = True

        # Write the compressed PDF to a temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
            writer.write(temp_file)
            temp_file_path = temp_file.name

        # Check the compression ratio achieved
        compressed_size = os.path.getsize(temp_file_path)
        compression_ratio = compressed_size / initial_size
        compression_percentage = (1 - compression_ratio) * 100

        # If compression increased file size or didn't meet minimum threshold, return original file
        if compression_ratio >= 1 or compression_percentage < 5:
            os.remove(temp_file_path)
            return input_file, f"Unable to compress the PDF effectively. Original file returned. (Attempted compression: {compression_percentage:.2f}%)"

        return temp_file_path, f"PDF compressed successfully! Compression achieved: {compression_percentage:.2f}%"
    except Exception as e:
        return None, f"Error compressing PDF: {str(e)}"

# The rest of the code remains the same

def process_and_compress(input_file, url, strength):
    sys.setrecursionlimit(10000)
    
    output_file, message = compress_pdf(input_file, url, strength)
    if output_file:
        return output_file, message
    else:
        return None, message

with gr.Blocks() as demo:
    gr.Markdown("# PDF Compressor")
    with gr.Row():
        input_file = gr.File(label="Upload PDF")
        url_input = gr.Textbox(label="Or enter PDF URL")
    strength = gr.Radio(["Low", "Medium", "High"], label="Compression Strength", value="Medium", info="Low: ~25% compression, Medium: ~50% compression, High: ~75% compression")
    compress_btn = gr.Button("Compress")
    output_file = gr.File(label="Download Compressed PDF")
    message = gr.Textbox(label="Message")

    compress_btn.click(
        process_and_compress,
        inputs=[input_file, url_input, strength],
        outputs=[output_file, message]
    )

if __name__ == "__main__":
    demo.launch(share=True)