Skip to content

Instantly share code, notes, and snippets.

@roadsideseb
Created January 30, 2014 23:16
Show Gist options
  • Save roadsideseb/8722203 to your computer and use it in GitHub Desktop.
Save roadsideseb/8722203 to your computer and use it in GitHub Desktop.
Packing slips for Oscar using PISA in the dashboard
from xhtml2pdf import pisa
from xhtml2pdf.pdf import pisaPDF
class OrderListView(ListView, BulkEditMixin):
...
actions = (
'generate_packing_slips',
...
)
...
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:
voucher_codes.append(discount.voucher_code)
context = RequestContext(request, {
'order': order,
'STATIC_ROOT': settings.STATIC_ROOT,
'voucher_codes': voucher_codes,
})
html = template.render(context)
result = StringIO()
order_pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result)
if order_pdf.err:
messages.error(
self.request,
_("An problem occured trying to generate the packing slip for "
"order #%s") % order.number,
)
else:
main_pdf.addDocument(order_pdf)
response = HttpResponse(main_pdf.getvalue(), mimetype='application/pdf')
filename = self.get_packing_slip_filename(orders)
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
@kyu0114
Copy link

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