Spaces:
Running
Running
File size: 1,746 Bytes
342e4c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import re
import base64
import streamlit as st
def html_to_pdf_bytes_playwright(html: str) -> bytes:
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.set_content(html, wait_until="load")
pdf_bytes = page.pdf(format="A4", print_background=True)
browser.close()
return pdf_bytes
def html_dowmload(full_report):
try:
pdf_bytes = html_to_pdf_bytes_playwright(full_report)
except Exception as e:
st.error(f"生成 PDF 出错:{e}")
else:
b64 = base64.b64encode(pdf_bytes).decode("utf-8")
auto_download_html = f"""
<html>
<body>
<a id="dl_link"
href="data:application/pdf;base64,{b64}"
download="report.pdf"
style="display:none">download</a>
<script>
(function() {{
const a = document.getElementById('dl_link');
try {{
a.click();
}} catch (err) {{
// 如果自动点击被阻止,替换页面内容并露出手动链接
document.body.innerHTML =
'<p>自动下载被浏览器阻止,请点击下面链接手动下载:</p>' + a.outerHTML;
}}
}})();
</script>
</body>
</html>
"""
st.components.v1.html(auto_download_html, height=120)
st.download_button(
label="⬇️ 手动下载 PDF(回退)",
data=pdf_bytes,
file_name="report.pdf",
mime="application/pdf",
)
st.success("PDF 已生成(如未自动下载,请使用上方手动下载按钮)。") |