This commit is contained in:
alazo 2017-10-30 22:03:31 +01:00
parent 96100f13c6
commit 2d62c40453
2 changed files with 48 additions and 30 deletions

View file

@ -6,11 +6,12 @@ from reportlab.platypus import SimpleDocTemplate, Spacer, Frame, Paragraph, Pref
from reportlab.platypus import Table, TableStyle, Image from reportlab.platypus import Table, TableStyle, Image
from reportlab.lib.pagesizes import A4, landscape from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.units import cm from reportlab.lib.units import cm
from reportlab.lib.enums import TA_LEFT, TA_CENTER from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
from reportlab.lib import colors from reportlab.lib import colors
from reportlab.lib.colors import HexColor from reportlab.lib.colors import HexColor
from reportlab.lib.styles import ParagraphStyle as ps from reportlab.lib.styles import ParagraphStyle as ps
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import stringWidth
style_8_c = ps(name='CORPS', fontName='Helvetica', fontSize=6, alignment=TA_CENTER) style_8_c = ps(name='CORPS', fontName='Helvetica', fontSize=6, alignment=TA_CENTER)
style_normal = ps(name='CORPS', fontName='Helvetica', fontSize=9, alignment=TA_LEFT) style_normal = ps(name='CORPS', fontName='Helvetica', fontSize=9, alignment=TA_LEFT)
@ -24,16 +25,41 @@ class MyDocTemplateES(SimpleDocTemplate):
if portrait is True: if portrait is True:
page_size = A4 page_size = A4
column_width = 8 * cm column_width = 8 * cm
my_frame = Frame(2*cm, 24*cm, 17*cm, 5*cm, showBoundary=1)
else: else:
page_size = landscape(A4) page_size = landscape(A4)
my_frame = Frame(2*cm, 16*cm, 25*cm, 5*cm, showBoundary=1)
column_width = 13 * cm column_width = 13 * cm
self.flowable = list()
SimpleDocTemplate.__init__(self, filename, pagesize=page_size, SimpleDocTemplate.__init__(self, filename, pagesize=page_size,
topMargin=0 * cm, topMargin=0 * cm,
leftMargin=2 * cm, leftMargin=2 * cm,
rightMargin=2 * cm, rightMargin=2 * cm,
bottomMargin=0.5 * cm, bottomMargin=0.5 * cm,
) )
self.fileName = filename self.fileName = filename
width, height = page_size
self.textWidth = width - 2*cm
self.canv = canvas.Canvas(filename, pagesize=page_size)
self.canv.drawImage(settings.MEDIA_ROOT + 'logo_EPC.png', 2 * cm, 17* cm, 5 * cm, 5 * cm,
preserveAspectRatio=True)
self.canv.drawImage(settings.MEDIA_ROOT + 'logo_ESNE.png', 14 * cm, 25.5 * cm, 5 * cm, 5 * cm,
preserveAspectRatio=True)
string_width = stringWidth(title_right, 'Helvetica-Bold', 12)
self.canv.setFont('Helvetica-Bold', 12)
self.canv.line(2 * cm, 25.5 * cm, 19 * cm, 25.5 * cm)
self.canv.drawString(2 * cm, 25 * cm, title_left)
self.canv.drawString(self.textWidth-string_width, 25*cm, title_right)
self.canv.line(2 * cm, 24.6 * cm, 19 * cm, 24.6 * cm)
self.canv.setFont('Helvetica', 10)
my_frame.addFromList(self.flowable, self.canv)
"""
self.fileName = filename
self.canv = canvas.Canvas(filename, pagesize=A4)
im1 = Image(settings.MEDIA_ROOT + 'logo_EPC.png', width=170, height=80) im1 = Image(settings.MEDIA_ROOT + 'logo_EPC.png', width=170, height=80)
im2 = Image(settings.MEDIA_ROOT + 'logo_ESNE.png', width=170, height=80) im2 = Image(settings.MEDIA_ROOT + 'logo_ESNE.png', width=170, height=80)
data = list() data = list()
@ -54,6 +80,8 @@ class MyDocTemplateES(SimpleDocTemplate):
) )
) )
self.flowable.append(t) self.flowable.append(t)
"""
def beforePage(self): def beforePage(self):
# page number # page number
@ -144,7 +172,6 @@ class ModulePdf(MyDocTemplateES):
class PlanFormationPdf(MyDocTemplateES): class PlanFormationPdf(MyDocTemplateES):
def __init__(self, filename): def __init__(self, filename):
self.flowable = list()
MyDocTemplateES.__init__(self, filename, 'Formation EDS', 'Plan de formation', portrait=False) MyDocTemplateES.__init__(self, filename, 'Formation EDS', 'Plan de formation', portrait=False)
def formating(self, el1='', length=40): def formating(self, el1='', length=40):
@ -152,6 +179,7 @@ class PlanFormationPdf(MyDocTemplateES):
return Preformatted(el1, style_normal, maxLineLength=length) return Preformatted(el1, style_normal, maxLineLength=length)
def produce(self, domain, process): def produce(self, domain, process):
my_frame = Frame(2*cm, 1*cm, 26*cm, 15*cm, showBoundary=1)
data = [ data = [
['Domaines', 'Processus', 'Sem1', 'Sem2', 'Sem3', 'Sem4', 'Sem5', 'Sem6'], ['Domaines', 'Processus', 'Sem1', 'Sem2', 'Sem3', 'Sem4', 'Sem5', 'Sem6'],
[self.formating(domain[0]), self.formating(process[0], 60), 'M01', '', '', '', '', ''], [self.formating(domain[0]), self.formating(process[0], 60), 'M01', '', '', '', '', ''],
@ -223,9 +251,10 @@ class PlanFormationPdf(MyDocTemplateES):
('BACKGROUND', (0, 14), (-1, 14), colors.lightgrey), ('BACKGROUND', (0, 14), (-1, 14), colors.lightgrey),
])) ]))
t.hAlign = TA_CENTER # t.hAlign = TA_CENTER
self.flowable.append(t) self.flowable.append(t)
self.build(self.flowable) my_frame.addFromList(self.flowable, self.canv)
#self.build(self.flowable)
class PDFResponse(HttpResponse): class PDFResponse(HttpResponse):
@ -258,19 +287,10 @@ class PeriodeFormationPdf(SimpleDocTemplate):
"""Imprime les heures de cours par semestre""" """Imprime les heures de cours par semestre"""
def __init__(self, filename): def __init__(self, filename):
self.flowable = list() self.flowable = list()
#SimpleDocTemplate.__init__(self, filename) #, 'Formation EDS', 'Périodes de la formation', portrait=True) MyDocTemplateES.__init__(self, filename, 'Filière EDS', 'Heures de formation', portrait=True)
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
im1 = Image(settings.MEDIA_ROOT + 'logo_EPC.png', width=120, height=80)
im2 = Image(settings.MEDIA_ROOT + 'logo_ESNE.png', width=120, height=80)
c.drawImage(settings.MEDIA_ROOT + 'logo_EPC.png', 0, height - 1*cm) # Who needs consistency?
c.drawImage(settings.MEDIA_ROOT + 'logo_ESNE.png', 8*cm, height-1*cm)
c.showPage()
c.save()
def produce_half_year(self, half_year_id, modules, total):
def produce_half_year(self, half_year_id, modules, total, canv): initial_pos_x = [2, 11.5]
initial_pos_x = [2, 11]
initial_pos_y = [17, 17, 10, 10, 3, 3] initial_pos_y = [17, 17, 10, 10, 3, 3]
width = 7.5*cm width = 7.5*cm
height = 6.5*cm height = 6.5*cm
@ -278,21 +298,21 @@ class PeriodeFormationPdf(SimpleDocTemplate):
x = initial_pos_x[(half_year_id-1) % 2]*cm x = initial_pos_x[(half_year_id-1) % 2]*cm
y = initial_pos_y[half_year_id-1]*cm y = initial_pos_y[half_year_id-1]*cm
my_frame = Frame(x, y, width, height, showBoundary=1) my_frame = Frame(x, y, width, height, showBoundary=0)
data = [['Semestre {0}'.format(half_year_id), '{0} h.'.format(total)]] data = [['Semestre {0}'.format(half_year_id), '{0} h.'.format(total)]]
for line in modules: for line in modules:
value = getattr(line, 'sem{0}'.format(half_year_id)) value = getattr(line, 'sem{0}'.format(half_year_id))
data.append([line.nom, '{0} h.'.format(value)]) data.append([line.nom, '{0} h.'.format(value)])
t = Table(data, colWidths=[7*cm, 1*cm], spaceBefore=0, spaceAfter=0, hAlign=TA_LEFT) t = Table(data, colWidths=[7*cm, 1*cm], spaceBefore=0.5*cm, spaceAfter=1*cm, hAlign=TA_LEFT)
t.setStyle(TableStyle([('ALIGN', (0, 0), (0, 0), 'LEFT'), t.setStyle(TableStyle([('ALIGN', (0, 0), (0, 0), 'LEFT'),
('ALIGN', (1, 0), (-1, -1), 'RIGHT'), ('ALIGN', (1, 0), (-1, -1), 'RIGHT'),
('LINEBELOW', (0, 0), (1, 0), 1, colors.black), ('LINEBELOW', (0, 0), (1, 0), 1, colors.black),
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ])) ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ]))
self.flowable.append(t) self.flowable.append(t)
my_frame.addFromList(self.flowable, canv) my_frame.addFromList(self.flowable, self.canv)
# self.build(self.flowable)
def print_total(self, total):
self.canv.drawString(2*cm, 2*cm, 'Total de la formation: {0} heures'.format(total))
def print_total(self, total, canv):
canv.drawString(2*cm, 2*cm, 'Total de la formation: {0} heures'.format(total))

View file

@ -116,6 +116,7 @@ def print_plan_formation(request):
domain = Domaine.objects.all().order_by('code') domain = Domaine.objects.all().order_by('code')
process = Processus.objects.all().order_by('code') process = Processus.objects.all().order_by('code')
pdf.produce(domain, process) pdf.produce(domain, process)
pdf.canv.save()
with open(path, mode='rb') as fh: with open(path, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/pdf') response = HttpResponse(fh.read(), content_type='application/pdf')
@ -126,21 +127,18 @@ def print_plan_formation(request):
def print_periode_formation(request): def print_periode_formation(request):
filename = 'periode_formation.pdf' filename = 'periode_formation.pdf'
path = os.path.join(tempfile.gettempdir(), filename) path = os.path.join(tempfile.gettempdir(), filename)
pdf = PeriodeFormationPdf(path) pdf = PeriodeFormationPdf(path)
context = {} context = {}
context = get_context(context) context = get_context(context)
canv = canvas.Canvas(filename)
for semestre_id in range(1, 7): for semestre_id in range(1, 7):
modules = context['sem{0}'.format(str(semestre_id))] modules = context['sem{0}'.format(str(semestre_id))]
total = context['tot{0}'.format(str(semestre_id))] total = context['tot{0}'.format(str(semestre_id))]
pdf.produce_half_year(semestre_id, modules, total, canv) pdf.produce_half_year(semestre_id, modules, total)
pdf.print_total(context['tot'])
pdf.canv.save()
pdf.print_total(context['tot'], canv) with open(path, mode='rb') as fh:
canv.save()
with open(filename, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/pdf') response = HttpResponse(fh.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename) response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)
return response return response