Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 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 (
|