【发布时间】:2021-02-04 17:59:13
【问题描述】:
我正在使用Django 项目并尝试使用WeasyPrint 呈现pdf。
我的views.py:
def WeasyPDF(request):
paragraphs = ['first paragraph', 'second paragraph', 'third paragraph']
html_string = render_to_string('app/pdf_report.html', {'paragraphs': paragraphs})
html = HTML(string=html_string)
html.write_pdf(
target='/tmp/mypdf.pdf',
stylesheets=[
# Change this to suit your css path
settings.BASE_DIR + 'css/bootstrap.min.css',
settings.BASE_DIR + 'css/main.css',
],
);
fs = FileSystemStorage('/tmp')
with fs.open('mypdf.pdf') as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="mypdf.pdf"'
return response
return response
但它说以下错误:
name 'HTML' is not defined
我正在关注本教程:link。
我该如何解决?
【问题讨论】:
-
你导入过这样的名字吗?
-
不,我没有导入那种东西。
-
那就难怪没有定义了...
-
from weasyprint import HTML来自WeasyPrint docs。看起来他们也有一个 Django 库:django-weasyprint