def impact_model_pdf(page, request, id): try: impact_model = ImpactModel.objects.get(id=id) except ImpactModel.DoesNotExist: messages.warning(request, 'Unknown model') return HttpResponseRedirect('/impactmodels/') pdf = render_impact_model_to_pdf(impact_model) return make_response(pdf)
def export_pdf(self): columns = [] for i in range(len(self._datatable.columns)): field = list(self._datatable.columns.keys())[i] col = self._datatable.columns[field] if not col.export or isinstance(col, DisplayColumn): continue columns.append(col) cabecera = [] for i in range(len(columns)): if not columns[i].export: continue cabecera.append(columns[i].label) datos = [] r = 0 for row in self._datatable.search(self.get_queryset()): d = [] for i in range(len(columns)): if not columns[i].export: continue if columns[i].processor: val = getattr(self._datatable, columns[i].processor)(row, self._datatable) else: val = row for attr in columns[i].sources[0].split('__'): try: val = getattr(val, attr) except: val = '' if isinstance(val, models.Model): val = str(val) if val is None: val = '' d.append(val) r += 1 datos.append(d) if hasattr(settings, 'LOGO_URL_DATATABLE'): logo_url = settings.LOGO_URL_DATATABLE else: logo_url = '' if hasattr(settings, 'MEDIA_ROOT'): static_url = settings.MEDIA_ROOT else: static_url = '' pdf = render_to_pdf( 'datatableview/pdf.html', { 'cabecera': cabecera, 'datos': datos, 'logo': logo_url, 'url': static_url }) return make_response(pdf, 'data.pdf')
def get_as_response(self): try: pdf = render_to_pdf(self.template_name, self.get_context()) response = make_response(pdf) response['Content-disposition'] = 'inline; filename="{}"'.format(self.file_name) return response except PDFRenderingError: error_message = 'Error trying to render PDF' logger.exception(error_message) return HttpResponse(error_message)