tomo2chin2 commited on
Commit
59180b5
·
verified ·
1 Parent(s): 8e0d60a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -15
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import gradio as gr
2
  import weasyprint
3
- import base64 # PDFをData URLに変換するために必要
 
 
 
4
 
5
  def display_html(html_code):
6
  return f"""
@@ -86,19 +89,36 @@ a:hover {
86
  }
87
  """
88
 
89
-
90
- def generate_pdf(html_code):
91
  """
92
- HTMLコードからPDFを生成し、Data URLとして返す。
93
  """
 
 
 
 
 
 
 
 
 
 
 
 
94
  html = weasyprint.HTML(string=html_code)
95
- pdf_bytes = html.write_pdf() # PDFをバイト列として生成
 
 
 
 
 
96
 
97
- # バイト列をBase64エンコードし、Data URLを作成
98
- pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
99
- pdf_data_url = f"data:application/pdf;base64,{pdf_base64}"
100
- return pdf_data_url
101
 
 
 
102
 
103
  with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
104
  gr.Markdown(
@@ -115,11 +135,12 @@ with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
115
  lines=10
116
  )
117
  with gr.Row():
118
- submit_button = gr.Button("表示", variant="primary")
119
- pdf_button = gr.Button("PDFで保存", variant="secondary") #PDFボタン追加
120
 
121
  with gr.Column(scale=3):
122
  output_html = gr.HTML(label="プレビュー")
 
123
 
124
 
125
  submit_button.click(
@@ -128,12 +149,12 @@ with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
128
  outputs=output_html
129
  )
130
 
131
- # PDFダウンロードボタンの処理
132
  pdf_button.click(
133
- generate_pdf,
134
- inputs=input_textbox, #input_textboxをPDFのインプットにする
135
- outputs=output_html #出力先をgr.htmlにする
136
  )
 
137
  #サンプル
138
  gr.Examples(
139
  [["<h1>Hello world</h1>"],["""
 
1
  import gradio as gr
2
  import weasyprint
3
+ import base64
4
+ import os
5
+ from bs4 import BeautifulSoup # HTML解析用
6
+
7
 
8
  def display_html(html_code):
9
  return f"""
 
89
  }
90
  """
91
 
92
+ def generate_pdf_and_link(html_code):
 
93
  """
94
+ HTMLからPDFを生成し、プレビューとダウンロードリンクを返す。
95
  """
96
+ # 1. HTMLプレビューの生成 (display_htmlを再利用)
97
+ preview_html = display_html(html_code)
98
+
99
+ # 2. PDFファイル名の決定
100
+ soup = BeautifulSoup(html_code, 'html.parser')
101
+ title_tag = soup.find('title')
102
+ if title_tag:
103
+ file_name = title_tag.string.strip() + ".pdf" # 空白文字を除去
104
+ else:
105
+ file_name = "output.pdf"
106
+
107
+ # 3. PDF生成
108
  html = weasyprint.HTML(string=html_code)
109
+ pdf_bytes = html.write_pdf()
110
+
111
+ # 4. PDFを一時ファイルとして保存 (Hugging Face Spaceでは/tmpが書き込み可能)
112
+ pdf_path = os.path.join('/tmp', file_name)
113
+ with open(pdf_path, 'wb') as f:
114
+ f.write(pdf_bytes)
115
 
116
+ # 5. ダウンロードリンクの作成
117
+ # GradioのFileコンポーネントを使ってダウンロードリンクを作る
118
+ download_link = pdf_path
 
119
 
120
+ # 6. プレビューとリンクを返す
121
+ return preview_html, download_link
122
 
123
  with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
124
  gr.Markdown(
 
135
  lines=10
136
  )
137
  with gr.Row():
138
+ submit_button = gr.Button("表示", variant="secondary") # スタイルを統一
139
+ pdf_button = gr.Button("PDFで保存", variant="secondary")
140
 
141
  with gr.Column(scale=3):
142
  output_html = gr.HTML(label="プレビュー")
143
+ output_pdf = gr.File(label="PDFダウンロード") # ファイル出力コンポーネント
144
 
145
 
146
  submit_button.click(
 
149
  outputs=output_html
150
  )
151
 
 
152
  pdf_button.click(
153
+ generate_pdf_and_link,
154
+ inputs=input_textbox,
155
+ outputs=[output_html, output_pdf] # プレビューとファイルを出力
156
  )
157
+
158
  #サンプル
159
  gr.Examples(
160
  [["<h1>Hello world</h1>"],["""