background image on pdf bulletins

This commit is contained in:
viennet 2020-10-21 22:56:25 +02:00
parent 9ce3af1c1d
commit 233a11ad14
6 changed files with 64 additions and 24 deletions

View File

@ -51,8 +51,7 @@ from sco_pdf import *
def mark_paras(L, tags): def mark_paras(L, tags):
"""Put each (string) element of L between <b> """Put each (string) element of L between <b>"""
"""
for tag in tags: for tag in tags:
b = "<" + tag + ">" b = "<" + tag + ">"
c = "</" + tag.split()[0] + ">" c = "</" + tag.split()[0] + ">"
@ -584,8 +583,7 @@ class GenTable:
return repr(doc) return repr(doc)
def json(self): def json(self):
"""JSON representation of the table. """JSON representation of the table."""
"""
d = [] d = []
for row in self.rows: for row in self.rows:
r = {} r = {}
@ -639,7 +637,9 @@ class GenTable:
return "\n".join(H) return "\n".join(H)
elif format == "pdf": elif format == "pdf":
objects = self.pdf() objects = self.pdf()
doc = pdf_basic_page(objects, title=title, preferences=self.preferences) doc = pdf_basic_page(
objects, title=title, preferences=self.preferences, context=context
)
if publish: if publish:
return sendPDFFile(REQUEST, doc, filename + ".pdf") return sendPDFFile(REQUEST, doc, filename + ".pdf")
else: else:

View File

@ -198,6 +198,7 @@ class BulletinGenerator:
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, document,
self.context,
author="%s %s (E. Viennet) [%s]" author="%s %s (E. Viennet) [%s]"
% (SCONAME, SCOVERSION, self.description), % (SCONAME, SCOVERSION, self.description),
title="Bulletin %s de %s" title="Bulletin %s de %s"

View File

@ -86,6 +86,7 @@ def pdfassemblebulletins(
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, document,
context,
author="%s %s (E. Viennet)" % (SCONAME, SCOVERSION), author="%s %s (E. Viennet)" % (SCONAME, SCOVERSION),
title="Bulletin %s" % bul_title, title="Bulletin %s" % bul_title,
subject="Bulletin de note", subject="Bulletin de note",
@ -107,13 +108,13 @@ def process_field(
"""Process a field given in preferences, returns """Process a field given in preferences, returns
- if format = 'pdf': a list of Platypus objects - if format = 'pdf': a list of Platypus objects
- if format = 'html' : a string - if format = 'html' : a string
Substitutes all %()s markup Substitutes all %()s markup
Remove potentialy harmful <img> tags Remove potentialy harmful <img> tags
Replaces <logo name="header" width="xxx" height="xxx"> Replaces <logo name="header" width="xxx" height="xxx">
by <img src=".../logos/logo_header" width="xxx" height="xxx"> by <img src=".../logos/logo_header" width="xxx" height="xxx">
If format = 'html', replaces <para> by <p>. HTML does not allow logos. If format = 'html', replaces <para> by <p>. HTML does not allow logos.
""" """
try: try:
text = (field or "") % WrapDict( text = (field or "") % WrapDict(
@ -136,7 +137,7 @@ def process_field(
# handle logos: # handle logos:
image_dir = SCODOC_LOGOS_DIR + "/logos_" + context.DeptId() + "/" image_dir = SCODOC_LOGOS_DIR + "/logos_" + context.DeptId() + "/"
if not os.path.exists(image_dir): if not os.path.exists(image_dir):
image_dir = SCODOC_LOGOS_DIR + "/" # use global logos image_dir = SCODOC_LOGOS_DIR + "/" # use global logos
text = re.sub( text = re.sub(
r"<(\s*)logo(.*?)src\s*=\s*(.*?)>", r"<\1logo\2\3>", text r"<(\s*)logo(.*?)src\s*=\s*(.*?)>", r"<\1logo\2\3>", text
) # remove forbidden src attribute ) # remove forbidden src attribute

View File

@ -159,6 +159,7 @@ class ScolarsPageTemplate(PageTemplate):
def __init__( def __init__(
self, self,
document, document,
context=None,
pagesbookmarks={}, pagesbookmarks={},
author=None, author=None,
title=None, title=None,
@ -178,6 +179,8 @@ class ScolarsPageTemplate(PageTemplate):
self.server_name = server_name self.server_name = server_name
self.filigranne = filigranne self.filigranne = filigranne
self.footer_template = footer_template self.footer_template = footer_template
self.with_page_background = self.preferences["bul_pdf_with_background"]
self.background_image_filename = None
# Our doc is made of a single frame # Our doc is made of a single frame
left, top, right, bottom = [float(x) for x in margins] left, top, right, bottom = [float(x) for x in margins]
content = Frame( content = Frame(
@ -188,9 +191,23 @@ class ScolarsPageTemplate(PageTemplate):
) )
PageTemplate.__init__(self, "ScolarsPageTemplate", [content]) PageTemplate.__init__(self, "ScolarsPageTemplate", [content])
self.logo = None self.logo = None
# XXX COPIED from sco_pvpdf, to be refactored (no time now)
# Search background in dept specific dir, then in global config dir
for image_dir in (
SCODOC_LOGOS_DIR + "/logos_" + context.DeptId() + "/",
SCODOC_LOGOS_DIR + "/", # global logos
):
for suffix in LOGOS_IMAGES_ALLOWED_TYPES:
fn = image_dir + "/bul_pdf_background" + "." + suffix
if not self.background_image_filename and os.path.exists(fn):
self.background_image_filename = fn
# Also try to use PV background
fn = image_dir + "/letter_background" + "." + suffix
if not self.background_image_filename and os.path.exists(fn):
self.background_image_filename = fn
def beforeDrawPage(self, canvas, doc): def beforeDrawPage(self, canvas, doc):
"""Draws a logo and an contribution message on each page. """Draws (optional) background, logo and contribution message on each page.
day : Day of the month as a decimal number [01,31] day : Day of the month as a decimal number [01,31]
month : Month as a decimal number [01,12]. month : Month as a decimal number [01,12].
@ -203,6 +220,12 @@ class ScolarsPageTemplate(PageTemplate):
""" """
canvas.saveState() canvas.saveState()
# ---- Background image
if self.background_image_filename and self.with_page_background:
canvas.drawImage(
self.background_image_filename, 0, 0, doc.pagesize[0], doc.pagesize[1]
)
# ---- Logo: a small image, positionned at top left of the page # ---- Logo: a small image, positionned at top left of the page
if self.logo is not None: if self.logo is not None:
# draws the logo if it exists # draws the logo if it exists
@ -266,7 +289,7 @@ def _makeTimeDict():
def pdf_basic_page( def pdf_basic_page(
objects, title="", preferences=None objects, title="", preferences=None, context=None
): # used by gen_table.make_page() ): # used by gen_table.make_page()
"""Simple convenience fonction: build a page from a list of platypus objects, """Simple convenience fonction: build a page from a list of platypus objects,
adding a title if specified. adding a title if specified.
@ -277,6 +300,7 @@ def pdf_basic_page(
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, document,
context,
title=title, title=title,
author="%s %s (E. Viennet)" % (SCONAME, SCOVERSION), author="%s %s (E. Viennet)" % (SCONAME, SCOVERSION),
footer_template="Edité par %(scodoc_name)s le %(day)s/%(month)s/%(year)s à %(hour)sh%(minute)s", footer_template="Edité par %(scodoc_name)s le %(day)s/%(month)s/%(year)s à %(hour)sh%(minute)s",

View File

@ -1386,6 +1386,16 @@ Année scolaire: %(anneescolaire)s
"category": "bul", "category": "bul",
}, },
), ),
(
"bul_pdf_with_background",
{
"initvalue": 0,
"title": "Mettre l'image de fond sur les bulletins",
"input_type": "boolcheckbox",
"labels": ["non", "oui"],
"category": "bul",
},
),
( (
"SCOLAR_FONT_BUL_FIELDS", "SCOLAR_FONT_BUL_FIELDS",
{ {

View File

@ -157,14 +157,16 @@ def trombino_html(context, groups_infos, REQUEST=None):
def check_local_photos_availability(context, groups_infos, REQUEST, format=""): def check_local_photos_availability(context, groups_infos, REQUEST, format=""):
"""Verifie que toutes les photos (des gropupes indiqués) sont copiées localement """Verifie que toutes les photos (des gropupes indiqués) sont copiées localement
dans ScoDoc (seules les photos dont nous disposons localement peuvent être exportées dans ScoDoc (seules les photos dont nous disposons localement peuvent être exportées
en pdf ou en zip). en pdf ou en zip).
Si toutes ne sont pas dispo, retourne un dialogue d'avertissement pour l'utilisateur. Si toutes ne sont pas dispo, retourne un dialogue d'avertissement pour l'utilisateur.
""" """
nb_missing = 0 nb_missing = 0
for t in groups_infos.members: for t in groups_infos.members:
etudid = t["etudid"] etudid = t["etudid"]
url = sco_photos.etud_photo_url(context, t, REQUEST=REQUEST) # -> copy distant files if needed url = sco_photos.etud_photo_url(
context, t, REQUEST=REQUEST
) # -> copy distant files if needed
if not sco_photos.etud_photo_is_local(context, t): if not sco_photos.etud_photo_is_local(context, t):
nb_missing += 1 nb_missing += 1
if nb_missing > 0: if nb_missing > 0:
@ -271,8 +273,7 @@ def trombino_copy_photos(context, group_ids=[], REQUEST=None, dialog_confirmed=F
def _get_etud_platypus_image(context, t, image_width=2 * cm): def _get_etud_platypus_image(context, t, image_width=2 * cm):
"""Returns aplatypus object for the photo of student t """Returns aplatypus object for the photo of student t"""
"""
try: try:
path = sco_photos.photo_pathname(context, t, size="small") path = sco_photos.photo_pathname(context, t, size="small")
if not path: if not path:
@ -364,7 +365,9 @@ def _trombino_pdf(context, groups_infos, REQUEST):
document = BaseDocTemplate(report) document = BaseDocTemplate(report)
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, preferences=context.get_preferences(sem["formsemestre_id"]) document,
context,
preferences=context.get_preferences(sem["formsemestre_id"]),
) )
) )
document.build(objects) document.build(objects)
@ -445,7 +448,9 @@ def _listeappel_photos_pdf(context, groups_infos, REQUEST):
document = BaseDocTemplate(report) document = BaseDocTemplate(report)
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, preferences=context.get_preferences(sem["formsemestre_id"]) document,
context,
preferences=context.get_preferences(sem["formsemestre_id"]),
) )
) )
document.build(objects) document.build(objects)
@ -509,7 +514,9 @@ def _listeappel_photos_pdf(context, groups_infos, REQUEST):
document = BaseDocTemplate(report) document = BaseDocTemplate(report)
document.addPageTemplates( document.addPageTemplates(
ScolarsPageTemplate( ScolarsPageTemplate(
document, preferences=context.get_preferences(sem["formsemestre_id"]) document,
context,
preferences=context.get_preferences(sem["formsemestre_id"]),
) )
) )
document.build(objects) document.build(objects)
@ -519,8 +526,7 @@ def _listeappel_photos_pdf(context, groups_infos, REQUEST):
# --------------------- Upload des photos de tout un groupe # --------------------- Upload des photos de tout un groupe
def photos_generate_excel_sample(context, group_ids=[], REQUEST=None): def photos_generate_excel_sample(context, group_ids=[], REQUEST=None):
"""Feuille excel pour import fichiers photos """Feuille excel pour import fichiers photos"""
"""
fmt = ImportScolars.sco_import_format() fmt = ImportScolars.sco_import_format()
data = ImportScolars.sco_import_generate_excel_sample( data = ImportScolars.sco_import_generate_excel_sample(
fmt, fmt,
@ -541,8 +547,7 @@ def photos_generate_excel_sample(context, group_ids=[], REQUEST=None):
def photos_import_files_form(context, group_ids=[], REQUEST=None): def photos_import_files_form(context, group_ids=[], REQUEST=None):
"""Formulaire pour importation photos """Formulaire pour importation photos"""
"""
groups_infos = sco_groups_view.DisplayedGroupsInfos( groups_infos = sco_groups_view.DisplayedGroupsInfos(
context, group_ids, REQUEST=REQUEST context, group_ids, REQUEST=REQUEST
) )
@ -596,8 +601,7 @@ def photos_import_files_form(context, group_ids=[], REQUEST=None):
def photos_import_files( def photos_import_files(
context, group_ids=[], xlsfile=None, zipfile=None, REQUEST=None context, group_ids=[], xlsfile=None, zipfile=None, REQUEST=None
): ):
"""Importation des photos """Importation des photos"""
"""
groups_infos = sco_groups_view.DisplayedGroupsInfos( groups_infos = sco_groups_view.DisplayedGroupsInfos(
context, group_ids, REQUEST=REQUEST context, group_ids, REQUEST=REQUEST
) )