GiantPandas commited on
Commit
d7d68f3
·
verified ·
1 Parent(s): d8b6426

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -312,11 +312,21 @@ def embed_file(file, scale):
312
  path = pathlib.Path(file.name)
313
  data = base64.b64encode(path.read_bytes()).decode()
314
  if path.suffix.lower() == ".pdf":
315
- # PDF:借浏览器内建 viewer,使用 #zoom=xxx 控制
316
- return (
317
- f'<iframe style="width:100%;height:800px;border:none" '
318
- f'src="data:application/pdf;base64,{data}#zoom={int(scale*100)}"></iframe>'
319
- )
 
 
 
 
 
 
 
 
 
 
320
  else:
321
  # 图片:CSS transform 实现 zoom;支持拖拽滚轮缩放
322
  return (
 
312
  path = pathlib.Path(file.name)
313
  data = base64.b64encode(path.read_bytes()).decode()
314
  if path.suffix.lower() == ".pdf":
315
+ b64 = base64.b64encode(pathlib.Path(file.name).read_bytes()).decode()
316
+ # 注意:把 JS/HTML 写在一起返回给 gr.HTML
317
+ return f"""
318
+ <script>
319
+ const pdf = atob("{b64}");
320
+ const bytes = Uint8Array.from(pdf, c => c.charCodeAt(0));
321
+ const blob = new Blob([bytes], {{type: "application/pdf"}});
322
+ const url = URL.createObjectURL(blob);
323
+ const iframe = document.getElementById("pdf_view") || document.createElement("iframe");
324
+ iframe.id = "pdf_view";
325
+ iframe.style = "width:100%;height:800px;border:none";
326
+ iframe.src = url + "#zoom={int(scale*100)}";
327
+ document.currentScript.parentElement.replaceChildren(iframe);
328
+ </script>
329
+ """
330
  else:
331
  # 图片:CSS transform 实现 zoom;支持拖拽滚轮缩放
332
  return (