Skip to content

Instantly share code, notes, and snippets.

Created Jan 30, 2014
What would you like to do?
Packing slips for Oscar using PISA in the dashboard
from xhtml2pdf import pisa
from xhtml2pdf.pdf import pisaPDF
class OrderListView(ListView, BulkEditMixin):
actions = (
def generate_packing_slips(self, request, orders):
template = loader.get_template(self.packing_slip_template)
main_pdf = pisaPDF()
for order in orders:
voucher_codes = []
for discount in order.discounts.all():
if discount.voucher_code:
context = RequestContext(request, {
'order': order,
'voucher_codes': voucher_codes,
html = template.render(context)
result = StringIO()
order_pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result)
if order_pdf.err:
_("An problem occured trying to generate the packing slip for "
"order #%s") % order.number,
response = HttpResponse(main_pdf.getvalue(), mimetype='application/pdf')
filename = self.get_packing_slip_filename(orders)
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response

This comment has been minimized.

Copy link

@kyu0114 kyu0114 commented Dec 7, 2017

Does this work in python 3?
Also how to define packing_slip_template and get_packing_slip_filename?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment