File size: 3,367 Bytes
0b5f517
 
 
 
 
 
 
9448e4d
 
0b5f517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from pdf2image import convert_from_bytes
from PIL import Image
import io
import fitz  # PyMuPDF for PDF handling
import docx
from fpdf import FPDF
import os
os.system("apt-get update && apt-get install -y poppler-utils")

st.set_page_config(page_title="PDF & Image Converter", page_icon="📄", layout="centered")
st.title("📄 PDF & Image Converter")
st.markdown("Convert PDFs to images, Word, and vice versa.")

# File uploader
uploaded_file = st.file_uploader("Upload a file (PDF, PNG, JPG, JPEG, DOCX)", type=["pdf", "png", "jpg", "jpeg", "docx"])

if uploaded_file:
    file_type = uploaded_file.type

    # ---------------- PDF TO IMAGE ----------------
    if file_type == "application/pdf":
        st.subheader("Convert PDF to Image")
        images = convert_from_bytes(uploaded_file.read())

        format_option = st.selectbox("Select image format", ["PNG", "JPEG", "JPG"])
        quality = st.slider("Image Quality", 50, 100, 90)

        zip_buffer = io.BytesIO()
        with st.spinner("Converting PDF to images..."):
            for i, img in enumerate(images):
                img_format = format_option.lower()
                img_byte_arr = io.BytesIO()
                img.save(img_byte_arr, format=img_format.upper(), quality=quality)
                st.image(img, caption=f"Page {i+1}", use_column_width=True)
                st.download_button(f"Download Page {i+1}", img_byte_arr.getvalue(), file_name=f"page_{i+1}.{img_format}")

    # ---------------- PDF TO WORD ----------------
        st.subheader("Convert PDF to Word")
        if st.button("Convert to DOCX"):
            doc = docx.Document()
            pdf_reader = fitz.open(stream=uploaded_file.read(), filetype="pdf")
            for page in pdf_reader:
                text = page.get_text()
                doc.add_paragraph(text)
            word_buffer = io.BytesIO()
            doc.save(word_buffer)
            st.download_button("Download DOCX", word_buffer.getvalue(), file_name="converted.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")

    # ---------------- IMAGE TO PDF ----------------
    elif file_type in ["image/png", "image/jpeg", "image/jpg"]:
        st.subheader("Convert Image to PDF")
        image = Image.open(uploaded_file)
        img_pdf_buffer = io.BytesIO()
        image.convert("RGB").save(img_pdf_buffer, format="PDF")
        st.download_button("Download PDF", img_pdf_buffer.getvalue(), file_name="converted.pdf", mime="application/pdf")

    # ---------------- WORD TO PDF ----------------
    elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        st.subheader("Convert Word to PDF")
        if st.button("Convert to PDF"):
            doc = docx.Document(uploaded_file)
            pdf = FPDF()
            pdf.set_auto_page_break(auto=True, margin=10)
            pdf.add_page()
            pdf.set_font("Arial", size=12)
            for para in doc.paragraphs:
                pdf.cell(200, 10, txt=para.text, ln=True)
            pdf_buffer = io.BytesIO()
            pdf.output(pdf_buffer, "F")
            st.download_button("Download PDF", pdf_buffer.getvalue(), file_name="converted.pdf", mime="application/pdf")

st.markdown("---")
st.markdown("💡 Developed with ❤️ using Streamlit, PyMuPDF, pdf2image, and PIL")