Spaces:
Sleeping
Sleeping
import gradio as gr | |
import subprocess | |
import os | |
def compress_pdf(input_pdf): | |
output_pdf = "/tmp/output.pdf" | |
# GhostscriptコマンドでPDFを軽量化 | |
result = subprocess.run( | |
["gs", "-sDEVICE=pdfwrite", | |
"-dPDFSETTINGS=/screen", # 軽量化レベル(/ebook や /printer も選べる) | |
"-dNOPAUSE", | |
"-dBATCH", | |
"-dQUIET", | |
f"-sOutputFile={output_pdf}", | |
input_pdf], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE | |
) | |
# エラー処理 | |
if result.returncode != 0: | |
return f"エラー: {result.stderr.decode()}", None | |
return "圧縮完了", output_pdf | |
# Gradioインターフェース | |
demo = gr.Interface( | |
fn=compress_pdf, | |
inputs=gr.File(file_types=[".pdf"]), | |
outputs=[gr.Text(), gr.File()], | |
title="PDF軽量化(Ghostscript)", | |
description="PDFファイルをGhostscriptで軽量化します(/screenモード)" | |
) | |
demo.launch() | |