K
K
KatyaBychko2022-02-21 15:20:06
Django
KatyaBychko, 2022-02-21 15:20:06

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

Please tell me what is my mistake.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nzrig, 2022-02-22
@KatyaBychko

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.

A
AVKor, 2022-02-22
@AVKor

You can try this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question