From 4e8e9b540702bed6463ca197050dc9ab5c40c4f6 Mon Sep 17 00:00:00 2001 From: Emanuel Schuetze Date: Sun, 29 Apr 2012 18:54:42 +0200 Subject: [PATCH] #155: Remove header/footer in frameless PDF. --- openslides/application/views.py | 8 +++++++- openslides/assignment/views.py | 22 ++++++++++++++++------ openslides/utils/views.py | 12 +++++++++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/openslides/application/views.py b/openslides/application/views.py index 4fca421e2..099cf8de9 100644 --- a/openslides/application/views.py +++ b/openslides/application/views.py @@ -33,7 +33,7 @@ from django.db import transaction from reportlab.lib import colors from reportlab.lib.units import cm -from reportlab.platypus import PageBreak, Paragraph, Spacer, Table, TableStyle +from reportlab.platypus import SimpleDocTemplate, PageBreak, Paragraph, Spacer, Table, TableStyle from config.models import config from settings import SITE_ROOT @@ -796,6 +796,12 @@ class ApplicationPollPDF(PDFView): filename = u'%s%s_%s' % (_("Application"), str(self.poll.application.number), _("Poll")) return filename + def get_template(self, buffer): + return SimpleDocTemplate(buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) + + def build_document(self, pdf_document, story): + pdf_document.build(story) + def append_to_pdf(self, story): imgpath = os.path.join(SITE_ROOT, 'static/images/circle.png') circle = "  " % imgpath diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index da8811824..ccb2e7537 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -20,7 +20,7 @@ from django.utils.translation import ugettext as _ from reportlab.lib import colors from reportlab.lib.units import cm -from reportlab.platypus import PageBreak, Paragraph, Spacer, Table, TableStyle +from reportlab.platypus import SimpleDocTemplate, PageBreak, Paragraph, Spacer, Table, TableStyle from config.models import config @@ -294,9 +294,17 @@ class AssignmentPollDelete(DeleteView): class AssignmentPDF(PDFView): permission_required = 'assignment.can_manage_assignment' - filename = u'filename=%s.pdf;' % _("Elections") top_space = 0 + def get_filename(self): + try: + assignment_id = self.kwargs['assignment_id'] + assignment = Assignment.objects.get(id=assignment_id) + filename = u'%s-%s' % (_("Assignment"), assignment.name.replace(' ','_')) + except: + filename = _("Elections") + return filename + def append_to_pdf(self, story): try: assignment_id = self.kwargs['assignment_id'] @@ -318,7 +326,6 @@ class AssignmentPDF(PDFView): story = self.get_assignment(assignment, story) else: # print selected assignment assignment = Assignment.objects.get(id=assignment_id) - filename = u'filename=%s-%s.pdf;' % (_("Assignment"), assignment.name.replace(' ','_')) story = self.get_assignment(assignment, story) def get_assignment(self, assignment, story): @@ -458,10 +465,13 @@ class AssignmentPollPDF(PDFView): filename = u'%s-%s-#%s' % (_("Election"), self.poll.assignment.name.replace(' ','_'), 1)#self.poll.get_ballot()) return filename - def append_to_pdf(self, story): - #doc = SimpleDocTemplate(response, pagesize=A4, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) - #story = [Spacer(0,0*cm)] + def get_template(self, buffer): + return SimpleDocTemplate(buffer, topMargin=-6, bottomMargin=-6, leftMargin=0, rightMargin=0, showBoundary=False) + def build_document(self, pdf_document, story): + pdf_document.build(story) + + def append_to_pdf(self, story): imgpath = os.path.join(SITE_ROOT, 'static/images/circle.png') circle = "  " % imgpath cell = [] diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 93bb82658..4365c5a27 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -238,22 +238,28 @@ class PDFView(PermissionMixin, View): def get_document_title(self): return self.document_title - def get_get_filename(self): + def get_filename(self): return self.filename + def get_template(self, buffer): + return SimpleDocTemplate(buffer) + + def build_document(self, pdf_document, story): + pdf_document.build(story, onFirstPage=firstPage, onLaterPages=laterPages) + def render_to_response(self, filename): response = HttpResponse(mimetype='application/pdf') filename = u'filename=%s.pdf;' % self.get_filename() response['Content-Disposition'] = filename.encode('utf-8') buffer = StringIO() - pdf_document = SimpleDocTemplate(buffer) + pdf_document = self.get_template(buffer) pdf_document.title = self.get_document_title() story = [Spacer(1, self.get_top_space()*cm)] self.append_to_pdf(story) - pdf_document.build(story, onFirstPage=firstPage, onLaterPages=laterPages) + self.build_document(pdf_document, story) pdf = buffer.getvalue() buffer.close()