简体   繁体   中英

django admin export to pdf

I want to export admin django to pdf (there is the normally export to csv I already have). What is the correct way to do this? There is some way in Python/Django: convert between CSV file to PDF or create a pdf file from this admin?

 def export_audits_as_pdf(self, request, queryset):

    file_name = "audit_entries{0}.pdf".format(time.strftime("%d-%m-%Y-%H-%M-%S"))
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file_name)

    data = [['Action Time', 'Priority', 'username', 'Source Address', 'Subject', 'Details']]
    for d in queryset.all():
        datetime_str = str(d.action_time).split('.')[0]
        item = [datetime_str, d.priority, d.username, d.source_address, d.subject, d.details]
        data.append(item)

    doc = SimpleDocTemplate(response, pagesize=(21*inch, 29*inch))
    elements = []

    table_data = Table(data)
    table_data.setStyle(TableStyle([('ALIGN', (0, 0), (-1, -1), 'LEFT'),
                                    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
                                    ("FONTSIZE",  (0, 0), (-1, -1), 13)]))
    elements.append(table_data)
    doc.build(elements)

    return response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM