mestreamlit / pages /2_📚_PDF预览.py
yuanjie
update
6207b87
raw
history blame
1.84 kB
from meutils.pipe import *
from appzoo.streamlit_app import Page
import streamlit as st
import streamlit.components.v1 as components
class MyPage(Page):
"""
https://zhuanlan.zhihu.com/p/518115802?utm_medium=social&utm_oi=1290068536690085888
"""
def main(self):
with st.form("PDF"):
file = st.file_uploader("选择待上传的PDF文件", type=['pdf', 'docx', 'xlsx'])
if st.form_submit_button('开始预览', help='先上传文件!!!'):
if file is not None:
base64_pdf = base64.b64encode(file.read()).decode('utf-8')
self.display_pdf(base64_pdf, width='100%', height=1000)
self.display_html()
def display_pdf(self, base64_pdf, width='100%', height=1000):
# https://blog.51cto.com/laok8/2395498
pdf_display = f"""<embed src="data:application/pdf;base64,{base64_pdf}" width="{width}" height="{height}" type="application/pdf">"""
# data_type = 'vnd.openxmlformats-officedocument.wordprocessingml.document'
# pdf_display = f"""<embed src="data:application/{data_type};base64,{base64_pdf}" width="{width}" height="{height}" type="application/{data_type}">"""
st.markdown(pdf_display, unsafe_allow_html=True)
def display_html(self, text='会飞的文字'):
_ = f"""
<marquee direction="down" width="100%" height="100%" behavior="alternate" style="border:solid" bgcolor="#00FF00">
<marquee behavior="alternate">
{text}
</marquee>
</marquee>
"""
st.markdown(_, unsafe_allow_html=True)
if __name__ == '__main__':
app_title = "# PDF应用"
app_info = "PDF预览"
MyPage(
app_title=app_title,
app_info=app_info,
sidebar_title=None,
).main()