Converting a html template containing highcharts to pdf

荒凉一梦 提交于 2020-08-10 19:17:20

问题


I have a html template with highcharts in it.I have to mail these reports after converting to pdf.I tried to use xhtml2pdf but it just loads everything but the charts.Here is my code (view.py)

from io import BytesIO
from xhtml2pdf import pisa
from schedule.settings import EMAIL_HOST_USER
from django.core.mail import EmailMessage


def sender1(request):
userid = request.user.email
template = get_template('html.html')
context = {
    'pagesize': 'A3',
    }  
html = template.render(context)
result = BytesIO()

pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)

email = EmailMessage('Sub', 'schedule body testing', EMAIL_HOST_USER, [userid])

email.attach('report.pdf', result.getvalue(), 'application/pdf')
email.send()

my django template looks like this

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="{% static 'node_modules/highcharts/highcharts.src.js' %}"></script>

<style type="text/css">
        @page {
            size: {{ pagesize }};
          }
        
    </style>
  </head>
 <body>
<div id="container1"></div>
<script>
Highcharts.chart('container1', {
  chart: {
      type: 'column'
  },
  title: {
      text: 'xyz'
  },
  credits: {
    enabled: false
},
  xAxis: {
      categories: [

       'tom','ron','Alex','John'
      ]
  },
  plotOptions: {
    series: {
        animation: false,
        dataLabels: {
            enabled: true
        }
    }
},

  series: [{
      name: 'A',
      data: [1,2,3,3],
    },{
      name: 'b',
      data: [2,5,6,8],
 }]
 });
</script>

 </body>
</html>

please help if there's any alternative to xhtml2pdf which can do this or with any modification which i should do to make xhtml2pdf work.

来源:https://stackoverflow.com/questions/63110952/converting-a-html-template-containing-highcharts-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!