File size: 1,013 Bytes
3b2d5b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()