File size: 1,609 Bytes
3b2d5b2
 
63d758b
3b2d5b2
63d758b
3b2d5b2
63d758b
 
faa9545
 
 
 
c45e793
 
 
faa9545
 
 
 
 
 
 
 
3b2d5b2
63d758b
 
 
 
 
 
 
 
 
 
 
3b2d5b2
 
 
63d758b
3b2d5b2
63d758b
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import subprocess
import os

def rasterize_and_report(input_pdf):
    output_pdf = "/tmp/output.pdf"

    # Ghostscriptコマンドで強制ラスタライズ
    cmd = [
        "gs",
        "-o", output_pdf,
        "-sDEVICE=pdfwrite",
        "-dColorImageResolution=150",
        "-dGrayImageResolution=150",
        "-dMonoImageResolution=150",
        "-dFILTERVECTOR",
        "-dCompatibilityLevel=1.4",
        "-dNOPAUSE",
        "-dBATCH",
        "-dQUIET",
        input_pdf
    ]
    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if result.returncode != 0:
        return f"Ghostscriptエラー: {result.stderr.decode()}", None, None

    if not os.path.exists(output_pdf):
        return "出力PDFが生成されませんでした", None, None

    # 出力ファイルサイズ取得
    size_bytes = os.path.getsize(output_pdf)
    size_kb = size_bytes / 1024
    size_str = f"{size_bytes} bytes ({size_kb:.1f} KB)"

    return f"変換完了 / ファイルサイズ: {size_str}", output_pdf, size_str


demo = gr.Interface(
    fn=rasterize_and_report,
    inputs=gr.File(file_types=[".pdf"]),
    outputs=[
        gr.Text(label="結果メッセージ"),
        gr.File(label="変換後PDF"),
        gr.Text(label="変換後ファイルサイズ")
    ],
    title="PDFラスタライズ(Ghostscript)",
    description=(
        "SVGなどのベクターPDFを強制的に画像化(ラスタライズ)してPDF出力します。"
        "Audiveris用にサイズを落とすのに便利です。"
    )
)

demo.launch()