Answer the question
In order to leave comments, you need to log in
Convert HTML to PDF?
Convert HTML to PDF (Django) . This is the first time I've done this, and I get the error cannot import name 'getStringIO' from 'reportlab.lib.utils'
from django.shortcuts import render
from mes.models import News
import io
from io import BytesIO
from django.template.loader import get_template
from django.http import HttpResponse
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type="application/pdf")
return None
def contact_pdf(request):
pdf = render_to_pdf('pdf.html')
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
content = 'inline; filename=contact.pdf'
response['Content-Disposition'] = content
return response
Answer the question
In order to leave comments, you need to log in
the problem is that getStringIO was removed in reportlab==3.6.7 if you want everything to work put reportlab==3.6.6. The guys have already opened a ticket on the git, so they will eliminate it in the future.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question