1

I am generating graph based reports and using Pygal library for it. I am able to get the graphs on html page but when I convert it into a pdf using wkhtml2pdf I am not able to get any graph.

I am rendering and putting the rendered charts in a list and then displaying them in a loop in my html template

def get_report_pdf(self, report, from_date=None, to_date=None):
    svgs = report.get_all_charts(from_date, to_date)
    html_string = loader.render_to_string('reporting/report- 
    template.html', {
        'charts': svgs
    })
    html = HTML(string=html_string)
    pdfkit.from_string(html_string, '/tmp/report- 
    {}.pdf'.format(report.id), options={'dpi': 300})

    fs = FileSystemStorage('/tmp')
    with fs.open('report-{}.pdf'.format(report.id)) as pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; 
        filename="report-{}.pdf"'.format(report.id)
        pdf.close()
        return response

My html template is as follows

<html>
<body>
{% for chart in charts %}
<div class="py">
     {{chart|safe}}
</div>
{% endfor %}
<body>
</html>

get_all_chart() method returns a list of rendered chart objects

I am getting a blank pdf currently. I want the svg charts to be displayed on it.

sky
  • 260
  • 3
  • 12
  • Does the `svgs` list contain a list of `` tags pointing at the charts, or does it contain a list of binary image objects? Since you're rendering it in the template, I assume it contains a list of the tags? – Will Keeling Jan 14 '19 at 13:30
  • it contains a list of binary image objects. – sky Jan 15 '19 at 08:05
  • 1
    Update: I got the solution, I added size to each chart and its now getting rendered on the pdf too, thanks for the help though :) – sky Jan 15 '19 at 08:06

0 Answers0