diff options
-rw-r--r-- | CHANGELOG.md | 15 | ||||
l---------[-rw-r--r--] | README | 22 | ||||
-rw-r--r-- | README.md | 31 | ||||
-rw-r--r-- | wkhtmltopdf/views.py | 58 |
4 files changed, 87 insertions, 39 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b2b7541 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,15 @@ +Changelog for django-wkhtmltopdf +================================ + + +Current Development +------------------- + +* Added option for orientation. Defaults to 'portrait', can be 'landscape'. +* Deprecated PdfTemplateView in preference of PDFTemplateView. +* Deprecated PdfResponse in preference of PDFResponse. +* Made PDFResponse more extensible. + + +0.1.1 +----- @@ -1,21 +1 @@ -Converts html to PDF -==================== - -Provides a thin wrapper to the wkhtmltopdf binary from http://code.google.com/p/wkhtmltopdf/ - -REQUIREMENTS -============ - -Install the binary http://code.google.com/p/wkhtmltopdf/ - This may require libfontconfig (on Ububtu: sudo aptitude install libfontconfig) - -INSTALLATION -============ - -Add 'wkhtmltopdf' to INSTALLED_APPS. - -By default it will execute the first wkhtmltopdf command found on your PATH. -You can set WKHTMLTOPDF_CMD to a specific execuatable: - -e.g. - WKHTMLTOPDF_CMD = '/path/to/my/wkhtmltopdf' +README.md
\ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..219d4e1 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +django-wkhtmltopdf +================== + + +Converts html to PDF +-------------------- + +Provides a thin wrapper to the wkhtmltopdf binary from http://code.google.com/p/wkhtmltopdf/ + + +Requirements +------------ + +Install the [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) binary. +This requires libfontconfig (on Ububtu: `sudo aptitude install libfontconfig`). + + +Installation +------------ + +Run `pip install django-wkhtmltopdf`. + +Add `'wkhtmltopdf'` to `INSTALLED_APPS` in your `settings.py`. + +By default it will execute the first wkhtmltopdf command found on your `PATH`. + +If you can't add wkhtmltopdf to your `PATH`, you can set `WKHTMLTOPDF_CMD` to a +specific execuatable: + +e.g.: in `settings.py` + WKHTMLTOPDF_CMD = '/path/to/my/wkhtmltopdf' diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 0c1ec4c..c26d1fc 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -8,48 +8,64 @@ from django.views.generic import TemplateView from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf -class PdfResponse(HttpResponse): + +class PDFResponse(HttpResponse): def __init__(self, content, filename): - super(PdfResponse, self).__init__(content, 'application/pdf') + super(PDFResponse, self).__init__(content, 'application/pdf') self.__setitem__('Content-Disposition', 'attachment; filename=%s' % filename) -class PdfTemplateView(TemplateView): +class PdfResponse(PDFResponse): + def __init__(self, content, filename): + warning = '''PdfResponse is deprecated in favour of PDFResponse. It will be removed in version 1.''' + raise PendingDeprecationWarning(warning) + super(PdfResponse, self).__init__(content, filename) + + +class PDFTemplateView(TemplateView): filename = 'rendered_pdf.pdf' footer_template = None header_template = None + orientation = 'portrait' margin_bottom = 0 margin_left = 0 margin_right = 0 margin_top = 0 - response = PdfResponse + response = PDFResponse def get(self, request, context_instance=None, *args, **kwargs): if request.GET.get('as', '') == 'html': - super(PdfTemplateView, self).get(request, *args, **kwargs) + return super(PDFTemplateView, self).get(request, *args, **kwargs) - page_path = template_to_temp_file(self.template_name, self.get_context_data(), context_instance) + self.context_instance = context_instance + page_path = template_to_temp_file(self.template_name, self.get_context_data(), self.context_instance) + pdf_kwargs = self.get_pdf_kwargs() + return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.get_filename()) + + def get_filename(self): + return self.filename + + def get_pdf_kwargs(self): + kwargs = { + 'margin_bottom': self.margin_bottom, + 'margin_left': self.margin_left, + 'margin_right': self.margin_right, + 'margin_top': self.margin_top, + 'orientation': self.orientation, + } tmp_files = [] if self.header_template: - kwargs['header_html'] = template_to_temp_file(self.header_template, self.get_context_data(), context_instance) + kwargs['header_html'] = template_to_temp_file(self.header_template, self.get_context_data(), self.context_instance) tmp_files.append(kwargs['header_html']) if self.footer_template: - kwargs['footer_html'] = template_to_temp_file(self.footer_template, self.get_context_data(), context_instance) + kwargs['footer_html'] = template_to_temp_file(self.footer_template, self.get_context_data(), self.context_instance) tmp_files.append(kwargs['footer_html']) - map(remove, tmp_files) - - kwargs.update({ - 'margin_bottom': self.margin_bottom, - 'margin_left': self.margin_left, - 'margin_right': self.margin_right, - 'margin_top': self.margin_top - }) - return self.response(wkhtmltopdf(page_path, **kwargs), self.filename) + return kwargs def get_context_data(self, **kwargs): - context = super(PdfTemplateView, self).get_context_data(**kwargs) + context = super(PDFTemplateView, self).get_context_data(**kwargs) match_full_url = compile(r'^https?://') if not match_full_url.match(settings.STATIC_URL): @@ -59,3 +75,9 @@ class PdfTemplateView(TemplateView): return context + +class PdfTemplateView(PDFTemplateView): #TODO: Remove this in v1.0 + def as_view(cls, **initkwargs): + warning = '''PdfTemplateView is deprecated in favour of PDFTemplateView. It will be removed in version 1.''' + raise PendingDeprecationWarning(warning) + return super(PdfTemplateView, cls).as_view(**initkwargs)
\ No newline at end of file |