0
ユーザーがプロファイルをpdfとしてダウンロードできるようにするWebアプリケーションを作成しようとしています。 @Robᵩ:Converting html to pdf using Python Flaskが提供するコードを使用しています。しかし、コードを実行すると、エラー "AttributeError: 'list'オブジェクトに 'エンコード'属性がありません。ここでFlaskでHTMLからPDFへの問題
は私のコードです:
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
誰もがこのエラーが発生することが原因となっているものを知っていますか?ありがとうございました。
完全トレースバック:ここ
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/jamespetullo/Downloads/MainSite-master2/studentsite1.py", line 190, in print_student_profile
pdf = file_class.render_pdf(html)
File "/Users/jamespetullo/Downloads/MainSite-master2/studentsite1.py", line 68, in render_pdf
pisa.CreatePDF(StringIO(html), pdf)
File "/Library/Python/2.7/site-packages/xhtml2pdf/document.py", line 89, in pisaDocument
encoding, context=context, xml_output=xml_output)
File "/Library/Python/2.7/site-packages/xhtml2pdf/document.py", line 57, in pisaStory
pisaParser(src, context, default_css, xhtml, encoding, xml_output)
File "/Library/Python/2.7/site-packages/xhtml2pdf/parser.py", line 685, in pisaParser
context.parseCSS()
File "/Library/Python/2.7/site-packages/xhtml2pdf/context.py", line 498, in parseCSS
self.css = self.cssParser.parse(self.cssText)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 434, in parse
src, stylesheet = self._parseStylesheet(src)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 522, in _parseStylesheet
src, stylesheetImports = self._parseAtImports(src)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 606, in _parseAtImports
stylesheet = self.cssBuilder.atImport(import_, mediums, self)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/css.py", line 874, in atImport
return cssParser.parseExternal(import_)
File "/Library/Python/2.7/site-packages/xhtml2pdf/context.py", line 380, in parseExternal
result = self.parse(cssFile.getData())
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 434, in parse
src, stylesheet = self._parseStylesheet(src)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 533, in _parseStylesheet
src, atResults = self._parseAtKeyword(src)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 655, in _parseAtKeyword
src, result = self._parseAtFontFace(src)
File "/Library/Python/2.7/site-packages/xhtml2pdf/w3c/cssParser.py", line 785, in _parseAtFontFace
result = [self.cssBuilder.atFontFace(properties)]
File "/Library/Python/2.7/site-packages/xhtml2pdf/context.py", line 173, in atFontFace
src = self.c.getFile(data["src"], relative=self.c.cssParser.rootPath)
File "/Library/Python/2.7/site-packages/xhtml2pdf/context.py", line 823, in getFile
return getFile(name, relative or self.pathDirectory)
File "/Library/Python/2.7/site-packages/xhtml2pdf/util.py", line 635, in getFile
file = pisaFileObject(*a, **kw)
File "/Library/Python/2.7/site-packages/xhtml2pdf/util.py", line 516, in __init__
uri = uri.encode('utf-8')
AttributeError: 'list' object has no attribute 'encode'
完全なトレースバックを投稿してください。そのため、エラーが発生した場所を推測する必要はありません。 – viddik13
[AttributeError: 'list'オブジェクトに重複する可能性のある属性は 'encode'ではありません](https://stackoverflow.com/questions/5054333/attributeerror-list-object-has-no-attribute-encode) –