Flas中HTML到PDF的问题

2024-05-08 20:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个允许用户以pdf格式下载他们的个人资料的web应用程序。我使用的是@Robᵩ:Converting html to pdf using Python Flask提供的代码。但是,当我运行代码时,我收到错误“AttributeError:'list'object has no attribute'encode'”。在

这是我的代码:

class Pdf:

   def render_pdf(self, html):

       from xhtml2pdf import pisa
       from StringIO import StringIO

       pdf = StringIO()

       pisa.CreatePDF(StringIO(html), pdf)

       return pdf.getvalue()

@app.route("/printmyprofile")
def print_student_profile():
    data = db_interaction.get_user_listing_by_email(user["email"])
    vals = ["name","personal_statement", "grade", "colleges", "majors", "skills", "activities"]
    html = flask.render_template('plain_profile.html', download=True, save=False, **{a:b.split(",") if a =="colleges" or a=="majors" or a == "skills" or a == "activities" else b for a, b in zip(vals, data[0][1:])})
    file_class = Pdf()
    pdf = file_class.render_pdf(html)
    headers = {'content-type': 'application.pdf', 'content-disposition': 'attachment; filename=certificate.pdf'}
    return pdf, 200

有人知道是什么引起了这个错误吗?谢谢您。在

完全回溯:

^{pr2}$

Tags: or代码fromimportreturnpdfdefhtml

热门问题