File size: 1,648 Bytes
0a43da7
 
 
 
742eede
0a43da7
 
35504c7
0a43da7
 
 
 
 
 
 
 
 
 
 
 
 
43518f8
0a43da7
 
 
43518f8
 
742eede
43518f8
 
 
 
0a43da7
43518f8
 
0a43da7
 
 
 
 
 
742eede
 
 
 
 
0a43da7
 
 
 
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 streamlit as st
from PIL import Image
from fpdf import FPDF
import io
import os

# Streamlit app
st.title("SlimShadow 🤗 Image to PDF Converter")

uploaded_files = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
page_numbering = st.checkbox("Enable Page Numbering")

if uploaded_files:
    images = []
    for file in uploaded_files:
        image = Image.open(file)
        images.append(image)
        
    if st.button("Generate PDF"):
        pdf = FPDF()
        for i, image in enumerate(images):
            # Save the image to a byte stream
            image_io = io.BytesIO()
            image.save(image_io, format="JPEG")
            image_io.seek(0)
            
            # Save the byte stream to a temporary file
            temp_filename = f'temp_image_{i}.jpg'
            with open(temp_filename, 'wb') as temp_file:
                temp_file.write(image_io.read())
            
            # Add a new page to the PDF and insert the image
            pdf.add_page()
            pdf.image(temp_filename, x=10, y=10, w=pdf.w - 20)
            
            # Add page number if enabled
            if page_numbering:
                pdf.set_y(-25)
                pdf.set_font("Arial", size=12)
                pdf.cell(0, 10, f'Page {i+1}', 0, 0, 'C')

            # Remove temporary file
            os.remove(temp_filename)

        # Get the PDF content as bytes
        pdf_output = io.BytesIO(pdf.output(dest='S').encode('latin1'))

        st.download_button("Download PDF", pdf_output, "images.pdf", "application/pdf")

        st.success("PDF Generated successfully!")