base PDF
This commit is contained in:
parent
2d62c40453
commit
061b909488
2 changed files with 103 additions and 20 deletions
119
cms/pdf.py
119
cms/pdf.py
|
|
@ -1,8 +1,9 @@
|
||||||
|
|
||||||
from django.http.response import HttpResponse
|
from django.http.response import HttpResponse
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from collections import OrderedDict
|
||||||
from reportlab.platypus import SimpleDocTemplate, Spacer, Frame, Paragraph, Preformatted
|
from reportlab.platypus import (SimpleDocTemplate, Spacer, Frame, Paragraph, Preformatted,
|
||||||
|
BaseDocTemplate, PageTemplate, NextPageTemplate)
|
||||||
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
|
||||||
|
|
@ -12,6 +13,7 @@ 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
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
||||||
|
from reportlab.lib.styles import getSampleStyleSheet
|
||||||
|
|
||||||
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)
|
||||||
|
|
@ -20,6 +22,61 @@ style_title = ps(name='CORPS', fontName='Helvetica', fontSize=12, alignment=TA_L
|
||||||
style_adress = ps(name='CORPS', fontName='Helvetica', fontSize=10, alignment=TA_LEFT, leftIndent=300)
|
style_adress = ps(name='CORPS', fontName='Helvetica', fontSize=10, alignment=TA_LEFT, leftIndent=300)
|
||||||
|
|
||||||
|
|
||||||
|
class MyESDocTemplate(BaseDocTemplate):
|
||||||
|
|
||||||
|
def __init__(self, filename, titles=['',''], pageSize=A4):
|
||||||
|
super().__init__(filename, pagesize=pageSize, _pageBreakQuick=0)
|
||||||
|
|
||||||
|
self.bottomMargin = 1*cm
|
||||||
|
self.letfMargin = 1.5*cm
|
||||||
|
self.titles = titles
|
||||||
|
self.page_width = (self.width + self.leftMargin * 2)
|
||||||
|
self.page_height = (self.height + self.bottomMargin * 2)
|
||||||
|
|
||||||
|
styles = getSampleStyleSheet()
|
||||||
|
|
||||||
|
# Setting up the frames, frames are use for dynamic content not fixed page elements
|
||||||
|
first_page_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width+1*cm, self.height - 2*cm,
|
||||||
|
id='small_table', showBoundary=0)
|
||||||
|
later_pages_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width+1*cm, self.height, id='large_table', showBoundary=0)
|
||||||
|
|
||||||
|
# Creating the page templates
|
||||||
|
first_page = PageTemplate(id='FirstPage', frames=[first_page_table_frame], onPage=self.on_first_page)
|
||||||
|
later_pages = PageTemplate(id='LaterPages', frames=[later_pages_table_frame], onPage=self.add_default_info)
|
||||||
|
self.addPageTemplates([first_page, later_pages])
|
||||||
|
# Tell Reportlab to use the other template on the later pages,
|
||||||
|
# by the default the first template that was added is used for the first page.
|
||||||
|
self.flowable =[NextPageTemplate(['*', 'LaterPages'])]
|
||||||
|
|
||||||
|
|
||||||
|
def on_first_page(self, canvas, doc):
|
||||||
|
canvas.saveState()
|
||||||
|
canvas.drawImage(settings.MEDIA_ROOT + 'logo_EPC.png', self.leftMargin, self.height - 1*cm, 5 * cm, 5 * cm,
|
||||||
|
preserveAspectRatio=True)
|
||||||
|
canvas.drawImage(settings.MEDIA_ROOT + 'logo_ESNE.png', self.width-2*cm, self.height - 1*cm, 5 * cm, 5 * cm,
|
||||||
|
preserveAspectRatio=True)
|
||||||
|
self._draw_title(canvas, self.page_height - 1.5 * cm)
|
||||||
|
# self.add_default_info(canvas, doc)
|
||||||
|
canvas.restoreState()
|
||||||
|
|
||||||
|
def add_default_info(self, canvas, doc):
|
||||||
|
canvas.saveState()
|
||||||
|
self._draw_title(canvas, self.page_height+1*cm)
|
||||||
|
canvas.restoreState()
|
||||||
|
|
||||||
|
def _draw_title(self, canvas, height):
|
||||||
|
width = self.leftMargin + self.width
|
||||||
|
x = self.leftMargin
|
||||||
|
y = height
|
||||||
|
canvas.line(x, y, width, y)
|
||||||
|
y -= 15
|
||||||
|
canvas.drawString(x, y, self.titles[0])
|
||||||
|
canvas.drawRightString(width, y, self.titles[1])
|
||||||
|
y -= 8
|
||||||
|
canvas.line(x, y, width, y)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MyDocTemplateES(SimpleDocTemplate):
|
class MyDocTemplateES(SimpleDocTemplate):
|
||||||
def __init__(self, filename, title_left, title_right, portrait=True):
|
def __init__(self, filename, title_left, title_right, portrait=True):
|
||||||
if portrait is True:
|
if portrait is True:
|
||||||
|
|
@ -91,17 +148,16 @@ class MyDocTemplateES(SimpleDocTemplate):
|
||||||
self.canv.restoreState()
|
self.canv.restoreState()
|
||||||
|
|
||||||
|
|
||||||
class ModulePdf(MyDocTemplateES):
|
class ModulePdf(MyESDocTemplate):
|
||||||
|
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
self.flowable = list()
|
super().__init__(filename, ['Formation EDS', 'Module de formation'])
|
||||||
MyDocTemplateES.__init__(self, filename, 'Formation EDS', 'Module de formation', portrait=True)
|
|
||||||
|
|
||||||
def preformatted_left(self, txt):
|
def preformatted_left(self, txt):
|
||||||
return Preformatted(txt, style_normal, maxLineLength=15)
|
return Preformatted(txt, style_normal, maxLineLength=15)
|
||||||
|
|
||||||
def preformatted_right(self, txt):
|
def preformatted_right(self, txt):
|
||||||
return Preformatted(txt, style_normal, maxLineLength=90)
|
return Preformatted(txt, style_normal, maxLineLength=97)
|
||||||
|
|
||||||
def produce(self, module):
|
def produce(self, module):
|
||||||
str_comp = ''
|
str_comp = ''
|
||||||
|
|
@ -132,7 +188,12 @@ class ModulePdf(MyDocTemplateES):
|
||||||
for l in lines:
|
for l in lines:
|
||||||
str_con += '{0}\n'.format(l)
|
str_con += '{0}\n'.format(l)
|
||||||
|
|
||||||
self.flowable.append(Spacer(0, 0.5 * cm))
|
lines = module.evaluation.split('\n')
|
||||||
|
str_con = ''
|
||||||
|
for l in lines:
|
||||||
|
str_con += '{0}\n'.format(l)
|
||||||
|
|
||||||
|
# self.flowable.append(Spacer(0, 0.5 * cm))
|
||||||
self.flowable.append(Paragraph(module.__str__(), style_bold, ))
|
self.flowable.append(Paragraph(module.__str__(), style_bold, ))
|
||||||
self.flowable.append(Spacer(0, 0.5 * cm))
|
self.flowable.append(Spacer(0, 0.5 * cm))
|
||||||
|
|
||||||
|
|
@ -156,10 +217,12 @@ class ModulePdf(MyDocTemplateES):
|
||||||
[self.preformatted_left('Responsable'),
|
[self.preformatted_left('Responsable'),
|
||||||
self.preformatted_right(module.processus.domaine.responsable.descr_pdf())],
|
self.preformatted_right(module.processus.domaine.responsable.descr_pdf())],
|
||||||
]
|
]
|
||||||
|
|
||||||
t = Table(data, colWidths=[3*cm, 13*cm])
|
t = Table(data, colWidths=[3*cm, 13*cm])
|
||||||
t.hAlign = TA_CENTER
|
t.hAlign = TA_CENTER
|
||||||
t.setStyle(
|
t.setStyle(
|
||||||
TableStyle([
|
TableStyle([
|
||||||
|
('SIZE', (0, 0), (-1, -1), 7),
|
||||||
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
|
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
|
||||||
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
||||||
('LEFTPADDING', (0, 0), (-1, -1), 0), ]
|
('LEFTPADDING', (0, 0), (-1, -1), 0), ]
|
||||||
|
|
@ -169,17 +232,17 @@ class ModulePdf(MyDocTemplateES):
|
||||||
self.build(self.flowable)
|
self.build(self.flowable)
|
||||||
|
|
||||||
|
|
||||||
class PlanFormationPdf(MyDocTemplateES):
|
class PlanFormationPdf(MyESDocTemplate):
|
||||||
|
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
MyDocTemplateES.__init__(self, filename, 'Formation EDS', 'Plan de formation', portrait=False)
|
super().__init__(filename, ['Formation EDS', 'Plan de formation'], landscape(A4))
|
||||||
|
|
||||||
def formating(self, el1='', length=40):
|
def formating(self, el1='', length=40):
|
||||||
el1 = '' if el1 == '' else el1.__str__()
|
el1 = '' if el1 == '' else el1.__str__()
|
||||||
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)
|
# 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', '', '', '', '', ''],
|
||||||
|
|
@ -251,10 +314,9 @@ class PlanFormationPdf(MyDocTemplateES):
|
||||||
('BACKGROUND', (0, 14), (-1, 14), colors.lightgrey),
|
('BACKGROUND', (0, 14), (-1, 14), colors.lightgrey),
|
||||||
|
|
||||||
]))
|
]))
|
||||||
# t.hAlign = TA_CENTER
|
t.hAlign = TA_LEFT
|
||||||
self.flowable.append(t)
|
self.flowable.append(t)
|
||||||
my_frame.addFromList(self.flowable, self.canv)
|
self.build(self.flowable)
|
||||||
#self.build(self.flowable)
|
|
||||||
|
|
||||||
|
|
||||||
class PDFResponse(HttpResponse):
|
class PDFResponse(HttpResponse):
|
||||||
|
|
@ -283,11 +345,33 @@ class PDFResponse(HttpResponse):
|
||||||
self.story.append(t)
|
self.story.append(t)
|
||||||
|
|
||||||
|
|
||||||
class PeriodeFormationPdf(SimpleDocTemplate):
|
class PeriodeFormationPdf(MyESDocTemplate):
|
||||||
"""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()
|
super().__init__(filename, ['Filière EDS', 'Heures de formation'])
|
||||||
MyDocTemplateES.__init__(self, filename, 'Filière EDS', 'Heures de formation', portrait=True)
|
|
||||||
|
def run(self):
|
||||||
|
table_grid = [["Product", "Quantity"]]
|
||||||
|
# Add the objects
|
||||||
|
self.objects = ['col1', 'col2'] * 50
|
||||||
|
for shipped_object in self.objects:
|
||||||
|
table_grid.append([shipped_object, "42"])
|
||||||
|
t = Table(table_grid, colWidths=[0.5 * self.width, 0.5 * self.width])
|
||||||
|
t.setStyle(TableStyle([('GRID', (0, 1), (-1, -1), 0.25, colors.gray),
|
||||||
|
('BOX', (0, 0), (-1, -1), 1.0, colors.black),
|
||||||
|
('BOX', (0, 0), (1, 0), 1.0, colors.black),
|
||||||
|
]))
|
||||||
|
self.flowable.append(t)
|
||||||
|
print(self.flowable)
|
||||||
|
"""
|
||||||
|
self.flowable.append(Table(table_grid, repeatRows=1, colWidths=[0.5 * self.width, 0.5 * self.width],
|
||||||
|
style=TableStyle([('GRID', (0, 1), (-1, -1), 0.25, colors.gray),
|
||||||
|
('BOX', (0, 0), (-1, -1), 1.0, colors.black),
|
||||||
|
('BOX', (0, 0), (1, 0), 1.0, colors.black),
|
||||||
|
])))
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.build(self.flowable)
|
||||||
|
|
||||||
def produce_half_year(self, half_year_id, modules, total):
|
def produce_half_year(self, half_year_id, modules, total):
|
||||||
initial_pos_x = [2, 11.5]
|
initial_pos_x = [2, 11.5]
|
||||||
|
|
@ -298,7 +382,7 @@ 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=0)
|
my_frame = Frame(x, y, width, height, showBoundary=1)
|
||||||
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))
|
||||||
|
|
@ -312,6 +396,7 @@ class PeriodeFormationPdf(SimpleDocTemplate):
|
||||||
|
|
||||||
self.flowable.append(t)
|
self.flowable.append(t)
|
||||||
my_frame.addFromList(self.flowable, self.canv)
|
my_frame.addFromList(self.flowable, self.canv)
|
||||||
|
self.canv.save()
|
||||||
|
|
||||||
def print_total(self, total):
|
def print_total(self, total):
|
||||||
self.canv.drawString(2*cm, 2*cm, 'Total de la formation: {0} heures'.format(total))
|
self.canv.drawString(2*cm, 2*cm, 'Total de la formation: {0} heures'.format(total))
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,6 @@ 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')
|
||||||
|
|
@ -135,8 +134,7 @@ def print_periode_formation(request):
|
||||||
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)
|
pdf.produce_half_year(semestre_id, modules, total)
|
||||||
pdf.print_total(context['tot'])
|
#pdf.print_total(context['tot'])
|
||||||
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')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue