2

Following my question : django oscar invoice pdf generation

I have finally managed to got it to work.

I had to download xhtml2pdf in python 3 version and added

    if isinstance(dest, BytesIO):
        data = data.encode("utf-8")

and then I added

def generate_packing_slips(self, request, orders):
    template = get_template('dashboard/orders/order_packing_slip.html')
    main_pdf = pisaPDF()

    for order in orders:
        voucher_codes = []
        for discount in order.discounts.all():
            if discount.voucher_code:
                voucher_codes.append(discount.voucher_code)

        context = {
            'order': order,
            'STATIC_ROOT': settings.STATIC_ROOT,
            'voucher_codes': voucher_codes,
        }
        html = template.render(context)
        result = BytesIO()

        order_pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result)

        if not order_pdf.err:
            main_pdf.addDocument(order_pdf)
            response = HttpResponse(result.getvalue(), content_type='application/pdf')
            filename = "orderinvoice.pdf"
            response['Content-Disposition'] = 'attachment; filename=%s' % filename
            return response

It works all fine and I can download the invoice as I intended to with the above template. However, I am trying to figure out how to get the filename as an order number. As per above code, all the invoice will download as orderinvoice.pdf. What would be the best way to set it so that the filename is (ordernumber).pdf?

Many Thanks!

Kyu

Hyun Kyu Han
  • 63
  • 1
  • 6
  • 1
    Okay. I spent some time to play around with the file and managed to figure out. `filename = "Invoice_%(ordernumber)s.pdf" % {'ordernumber': order.number}` was what I needed! – Hyun Kyu Han Dec 28 '17 at 00:17

0 Answers0