Spaces:
Sleeping
Sleeping
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", | |
"-dFILTERVECTOR", | |
"-dCompatibilityLevel=1.4", | |
"-dPDFSETTINGS=/printer", | |
"-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() | |