How to generate pdf from a Django page with graphics, images and text (result of an Assessment)

Asked

Viewed 107 times

2

I am developing an application with Django and need to display the result of an Assessment (test) that will have text, images and graphics. Initially I did this part using Chart.js based on this example: https://youtu.be/1OL5n06kO_w

The problem is that I also need the option to generate a pdf from this test result page while maintaining the formatting and graphics. I can generate pdf with images and text, but it loses the formatting of the text and does not display the charts of Chart.js.

I need to change how to generate the chart by abandoning Chart.js or the problem is how I am trying to generate the pdf by stepping.pisaDocument as below:

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
  • It is worth mentioning that I tried several different ways to generate the pdf. So I wonder if I should not change the way the graphics.

  • Create page with what you need with Graphic images text in the pattern you need and use https://weasyprint.org/ ... It mounts the pdf using the html css instructions

1 answer

-2

just left to add result.seek(0) below the variable pdf as in the example below

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)
    result.seek(0)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

Browser other questions tagged

You are not signed in. Login or sign up in order to post.