import gradio as gr import subprocess import os import tempfile def rasterize_with_pdftoppm(input_pdf): with tempfile.TemporaryDirectory() as tmpdir: output_prefix = os.path.join(tmpdir, "page") cmd1 = ["pdftoppm", "-png", "-r", "150", input_pdf, output_prefix] res1 = subprocess.run(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if res1.returncode != 0: return f"pdftoppm変換エラー: {res1.stderr.decode()}", None, None # PNGファイル一覧取得 png_files = sorted([os.path.join(tmpdir, f) for f in os.listdir(tmpdir) if f.endswith(".png")]) if not png_files: return "PNG変換に失敗しました", None, None # PNG→PDF変換(ImageMagickのconvertが使えなければpypdfで合成も可能) output_pdf = os.path.join(tmpdir, "output_rasterized.pdf") cmd2 = ["convert"] + png_files + [output_pdf] res2 = subprocess.run(cmd2, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if res2.returncode != 0: return f"PDF作成エラー: {res2.stderr.decode()}", 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 "pdftoppmでラスタライズ完了", output_pdf, size_str demo = gr.Interface( fn=rasterize_with_pdftoppm, inputs=gr.File(file_types=[".pdf"]), outputs=[gr.Text(), gr.File(), gr.Text()], title="PDFをpdftoppmでラスタライズ", description="pdftoppmでPDFをPNGに変換し再度PDF化します。" ) demo.launch()