File size: 5,294 Bytes
f239f65
4cf1b61
 
 
f239f65
4cf1b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
from pypdf import PdfWriter, PdfReader
from PIL import Image
import io

# --- PAGE CONFIGURATION ---
st.set_page_config(
    page_title="PDF & Image Toolkit",
    page_icon="πŸ“„",
    layout="centered",
)

# --- HELPER FUNCTIONS ---
def merge_pdfs(uploaded_files):
    """Merges multiple PDF files into one."""
    merger = PdfWriter()
    for pdf_file in uploaded_files:
        # pypdf needs a file object, so we wrap the uploaded bytes in BytesIO
        merger.append(io.BytesIO(pdf_file.getvalue()))
    
    # Write the merged PDF to a BytesIO object in memory
    output_buffer = io.BytesIO()
    merger.write(output_buffer)
    merger.close()
    return output_buffer.getvalue()

def compress_pdf(uploaded_file):
    """Performs basic compression on a PDF file."""
    reader = PdfReader(io.BytesIO(uploaded_file.getvalue()))
    writer = PdfWriter()

    for page in reader.pages:
        # This is a basic form of compression
        page.compress_content_streams()
        writer.add_page(page)

    # Write the compressed PDF to a BytesIO object
    output_buffer = io.BytesIO()
    writer.write(output_buffer)
    writer.close()
    return output_buffer.getvalue()

def images_to_pdf(uploaded_files):
    """Converts a list of images to a single PDF."""
    images = []
    for img_file in uploaded_files:
        img = Image.open(io.BytesIO(img_file.getvalue()))
        # Convert to RGB to avoid issues with different image modes (e.g., RGBA, P)
        if img.mode == 'RGBA' or img.mode == 'P':
            img = img.convert('RGB')
        images.append(img)
    
    if not images:
        return None

    # Save the images as a PDF to a BytesIO object
    output_buffer = io.BytesIO()
    images[0].save(
        output_buffer, 
        "PDF" ,
        resolution=100.0, 
        save_all=True, 
        append_images=images[1:]
    )
    return output_buffer.getvalue()


# --- STREAMLIT UI ---

st.title("πŸ“„ PDF & Image Toolkit")
st.write("A simple web app to manage your documents. All processing is done in your browser and on the server, and your files are not saved.")

# --- SIDEBAR FOR TOOL SELECTION ---
with st.sidebar:
    st.header("Tools")
    tool_choice = st.radio(
        "Select a tool:",
        ("Merge PDFs", "Compress PDF", "Convert Images to PDF")
    )

# --- MAIN PAGE CONTENT ---

if tool_choice == "Merge PDFs":
    st.subheader("Merge Multiple PDF Files")
    st.write("Upload two or more PDF files to combine them into a single document.")
    
    uploaded_pdfs = st.file_uploader(
        "Upload PDF files", 
        type="pdf", 
        accept_multiple_files=True,
        key="pdf_merger"
    )

    if st.button("Merge PDFs") and uploaded_pdfs:
        if len(uploaded_pdfs) < 2:
            st.warning("Please upload at least two PDF files to merge.")
        else:
            with st.spinner("Merging..."):
                merged_pdf_bytes = merge_pdfs(uploaded_pdfs)
                st.success("PDFs merged successfully!")
                st.download_button(
                    label="Download Merged PDF",
                    data=merged_pdf_bytes,
                    file_name="merged_document.pdf",
                    mime="application/pdf",
                )

elif tool_choice == "Compress PDF":
    st.subheader("Compress a PDF File")
    st.info("This tool performs basic optimization. It may not significantly reduce the size of all PDFs, especially those that are already optimized or text-heavy.")

    uploaded_pdf = st.file_uploader(
        "Upload a PDF file", 
        type="pdf", 
        accept_multiple_files=False,
        key="pdf_compressor"
    )

    if st.button("Compress PDF") and uploaded_pdf:
        with st.spinner("Compressing..."):
            compressed_pdf_bytes = compress_pdf(uploaded_pdf)
            original_size = uploaded_pdf.size
            compressed_size = len(compressed_pdf_bytes)
            reduction = ((original_size - compressed_size) / original_size) * 100
            
            st.success(f"Compression complete! Size reduced by {reduction:.2f}%.")
            st.download_button(
                label="Download Compressed PDF",
                data=compressed_pdf_bytes,
                file_name="compressed_document.pdf",
                mime="application/pdf",
            )


elif tool_choice == "Convert Images to PDF":
    st.subheader("Convert Images to a Single PDF")
    st.write("Upload one or more images (JPG, PNG) to combine them into a PDF.")

    uploaded_images = st.file_uploader(
        "Upload image files",
        type=["png", "jpg", "jpeg"],
        accept_multiple_files=True,
        key="img_converter"
    )

    if st.button("Convert to PDF") and uploaded_images:
        with st.spinner("Converting..."):
            pdf_from_images_bytes = images_to_pdf(uploaded_images)
            st.success("Images converted to PDF successfully!")
            st.download_button(
                label="Download PDF",
                data=pdf_from_images_bytes,
                file_name="images_converted.pdf",
                mime="application/pdf",
            )

st.sidebar.markdown("---")
st.sidebar.info("App built with [Streamlit](https://streamlit.io) and hosted on [Hugging Face Spaces](https://huggingface.co/spaces).")