shri narayan yadav
shri narayan yadav

Reputation: 29

create pdf and insert into Django data base i am using python 3.8 and Django 3.1.7 and xhtml2pdf==0.2.5

here is my code

def user_invoice_pdf(request,queryset):
    template_path = 'user/order_details.html'
    context = {'product':queryset}
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="invoice.pdf"'
    template = get_template(template_path)
    html = template.render(context)
    pdf = render_to_pdf(template_path,context)
    if pdf:
        receipt_file = File(BytesIO(pdf.response))
        print(receipt_file,'receipt_file')
        invoice = InvoiceData(user_info=request.userinvoie=receipt_file)
        invoice.save()
    else:
        return response
    return True

Upvotes: 0

Views: 67

Answers (1)

shri narayan yadav
shri narayan yadav

Reputation: 29

try this

file_name = first_name
file_name2 = file_name+'.'+ "pdf"
user_invoice_obj=Modelsname(user=request.user,order_product=queryset)
user_invoice_obj.save()
user_invoice_obj.invoice.save(file_name2, File(BytesIO(pdf)))

Upvotes: 1

Related Questions