python 如何在Streamlit中转换和下载markdown文本作为PDF报告?

vof42yt1  于 6个月前  发布在  Python
关注(0)|答案(2)|浏览(109)

我在Streamlit中生成了markdown文本,如下所示。

st_md = '''
<b>compare mongodb to other no sql databases</b><br><br><b>Uploaded Files: </b>[]<br><br> Here is a comparison of MongoDB to some other major NoSQL databases:

- MongoDB is a document database. It stores data in flexible JSON-like documents rather than rows and columns like an RDBMS. Other document databases include CouchDB and Amazon DocumentDB.

In summary, MongoDB strikes a balance between the flexibility of document storage, rich functionality like secondary indexes and aggregations, and scalability via horizontal sharding that makes it a popular choice among many NoSQL databases today.<br><br><b>advantages and disadvantages of mongodb to other no sql ds</b><br><br><b>Uploaded Files: </b>[]<br><br> Here are some key advantages and disadvantages of MongoDB compared to other NoSQL databases:

Advantages:

- Flexible data model using documents to represent objects with dynamic schemas. More flexible than columnar databases that require predefined schemas.

- Index on any attribute for faster queries and retrieval compared to key-value stores.

Disadvantages:

- Less ACID compliance and transactions than traditional SQL databases.

- No declarative query language like SQL. Query syntax can be complex for some use cases.

So in summary, MongoDB provides a flexible document data model with rich functionality leading to faster reads and more expressiveness compared to simple key-value stores, but lacks some features database specialists may require. Scaling and performance is generally easier than traditional SQL databases.<br><br>
'''

字符串
我想将st_md的内容下载到pdf文件中。我尝试使用download_button,但收到一个错误,即pdf文件已损坏。我缺少什么?

st.download_button(
    label="Download data as pdf",
    data=st_md,
    file_name='test.pdf',
)

utugiqy6

utugiqy61#

st.download_button函数用于下载文件,但它可能无法直接处理Markdown文本到PDF的转换。为了实现您的目标,您可以使用reportlab库将Markdown文本转换为PDF文件。

import streamlit as st
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from io import BytesIO
import base64

# Your markdown text
st_md = '''
# Your Markdown content
'''

# Function to convert Markdown to PDF
def markdown_to_pdf(markdown_text):
    buffer = BytesIO()
    p = canvas.Canvas(buffer, pagesize=letter)
    p.drawString(100, 800, markdown_text)  # Adjust the position as needed
    p.save()
    buffer.seek(0)
    return buffer

# Get the PDF buffer
pdf_buffer = markdown_to_pdf(st_md)

# Display the download button
st.download_button(
    label="Download data as pdf",
    data=pdf_buffer,
    file_name='test.pdf',
)

# Optional: Display the PDF content in Streamlit
st.image(pdf_buffer, format='pdf')

# Close the buffer to free up resources
pdf_buffer.close()

字符串

yr9zkbsy

yr9zkbsy2#

使用@Chathura Abeywickrama的建议,我使用下面的代码将我的markdown内容转换为pdf:

html = markdown2.markdown(st_md)
pdfkit.from_string(html, 'example.pdf')

字符串
然后在download_button中使用生成的pdf文件

with open("example.pdf", "rb") as f:
    st.download_button("Download Conversation", f, f"example.pdf")

相关问题