Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import subprocess | |
| import os | |
| import shutil | |
| def kompres_pdf(input_file, tingkat_kompresi): | |
| if input_file is None: | |
| return None, "Silakan unggah file PDF terlebih dahulu." | |
| input_path = input_file.name | |
| output_path = "hasil_kompresi.pdf" | |
| # Pemetaan kualitas kompresi | |
| kualitas_map = { | |
| "Sangat Kecil (Resolusi Layar/Web)": '/screen', | |
| "Kecil (Kualitas Ebook)": '/ebook', | |
| "Sedang (Kualitas Printer)": '/printer', | |
| "Bagus (Kualitas Prepress)": '/prepress', | |
| "Bawaan (Default)": '/default' | |
| } | |
| kualitas_gs = kualitas_map[tingkat_kompresi] | |
| perintah_gs = [ | |
| "gs", | |
| "-sDEVICE=pdfwrite", | |
| "-dCompatibilityLevel=1.4", | |
| f"-dPDFSETTINGS={kualitas_gs}", | |
| "-dNOPAUSE", | |
| "-dQUIET", | |
| "-dBATCH", | |
| f"-sOutputFile={output_path}", | |
| input_path | |
| ] | |
| try: | |
| # Jalankan Ghostscript | |
| subprocess.run(perintah_gs, check=True) | |
| # Hitung ukuran file | |
| ukuran_awal = os.path.getsize(input_path) / (1024 * 1024) | |
| ukuran_akhir = os.path.getsize(output_path) / (1024 * 1024) | |
| pesan = f"Sukses! Ukuran Awal: {ukuran_awal:.2f} MB ➡️ Ukuran Akhir: {ukuran_akhir:.2f} MB" | |
| return output_path, pesan | |
| except Exception as e: | |
| return None, f"Terjadi kesalahan: {str(e)}" | |
| # Membuat Tampilan Web dengan Gradio | |
| with gr.Blocks(title="Kompres PDF Gratis") as app: | |
| gr.Markdown("# 🗜️ Kompres PDF Gratis 100%") | |
| gr.Markdown("Kecilkan ukuran file PDF Anda tanpa kehilangan teks aslinya. Aman, gratis, dan cepat!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| file_input = gr.File(label="Unggah File PDF", file_types=[".pdf"]) | |
| opsi_kompresi = gr.Radio( | |
| choices=[ | |
| "Sangat Kecil (Resolusi Layar/Web)", | |
| "Kecil (Kualitas Ebook)", | |
| "Sedang (Kualitas Printer)", | |
| "Bagus (Kualitas Prepress)", | |
| "Bawaan (Default)" | |
| ], | |
| value="Sangat Kecil (Resolusi Layar/Web)", | |
| label="Pilih Tingkat Kompresi" | |
| ) | |
| tombol_kompres = gr.Button("Kompres Sekarang!", variant="primary") | |
| with gr.Column(): | |
| file_output = gr.File(label="Hasil Kompresi (Siap Diunduh)") | |
| teks_status = gr.Textbox(label="Status", interactive=False) | |
| tombol_kompres.click( | |
| fn=kompres_pdf, | |
| inputs=[file_input, opsi_kompresi], | |
| outputs=[file_output, teks_status] | |
| ) | |
| app.launch() |