summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/income/views.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/app/income/views.py b/app/income/views.py
index 69ca1fd..2c3e34a 100644
--- a/app/income/views.py
+++ b/app/income/views.py
@@ -1,8 +1,10 @@
import datetime
from django.views.generic.detail import DetailView
+from django.template.loader import render_to_string
+from django.http import HttpResponse
+from django.conf import settings
-from templated_docs import fill_template
-from templated_docs.http import FileResponse
+from weasyprint import HTML, CSS
from .models import Invoice, InvoiceItem
@@ -31,8 +33,14 @@ class DownloadMonthlyInvoiceView(MonthlyInvoiceView):
slug_field = "slug"
def get(self, *args, **kwargs):
+ import logging
+ logger = logging.getLogger('weasyprint')
+ logger.addHandler(logging.FileHandler('weasyprint.log'))
self.object = self.get_object() # assign the object to the view
- obj = self.get_context_data()
- filename = fill_template('invoice.odt', obj, output_format='pdf')
- visible_filename = 'invoice.{}'.format('pdf')
- return FileResponse(filename, visible_filename)
+ c = {'object': self.object}
+ t = render_to_string('details/invoice.html', c).encode('utf-8')
+ html = HTML(string=t, base_url=self.request.build_absolute_uri())
+ pdf = html.write_pdf(stylesheets=[CSS(settings.MEDIA_ROOT + 'site/media/pdf_gen.css')], presentational_hints=True)
+ response = HttpResponse(pdf, content_type='application/pdf')
+ response['Content-Disposition'] = 'inline; filename="invoice.pdf"'
+ return response