2020-09-26 16:19:37 +02:00
|
|
|
|
# -*- mode: python -*-
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
|
#
|
2023-01-02 13:16:27 +01:00
|
|
|
|
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
|
2020-09-26 16:19:37 +02:00
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
#
|
|
|
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
|
|
"""Generation de PDF: définitions diverses et gestion du verrou
|
|
|
|
|
|
|
|
|
|
reportlab n'est pas réentrante: il ne faut qu'une seule opération PDF au même moment.
|
|
|
|
|
Tout accès à ReportLab doit donc être précédé d'un PDFLOCK.acquire()
|
|
|
|
|
et terminé par un PDFLOCK.release()
|
2021-08-22 07:47:06 +02:00
|
|
|
|
|
2023-08-30 16:03:36 +02:00
|
|
|
|
En ScoDoc 9, ce n'est pas nécessaire car on est multiprocessus / monothread.
|
2020-09-26 16:19:37 +02:00
|
|
|
|
"""
|
2023-08-30 16:03:36 +02:00
|
|
|
|
import datetime
|
2021-09-07 23:54:33 +02:00
|
|
|
|
import html
|
2021-07-25 22:31:59 +02:00
|
|
|
|
import io
|
2021-01-04 13:53:19 +01:00
|
|
|
|
import os
|
2021-08-22 07:47:06 +02:00
|
|
|
|
import queue
|
|
|
|
|
import re
|
|
|
|
|
import threading
|
2020-09-26 16:19:37 +02:00
|
|
|
|
import traceback
|
2021-08-22 07:47:06 +02:00
|
|
|
|
import unicodedata
|
2021-07-11 22:56:22 +02:00
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
import reportlab
|
2022-03-24 09:59:44 +01:00
|
|
|
|
from reportlab.pdfgen import canvas
|
|
|
|
|
from reportlab.platypus import Paragraph, Frame
|
2020-09-26 16:19:37 +02:00
|
|
|
|
from reportlab.platypus.flowables import Flowable
|
|
|
|
|
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
|
2021-01-01 18:40:47 +01:00
|
|
|
|
from reportlab.rl_config import defaultPageSize # pylint: disable=no-name-in-module
|
2020-09-26 16:19:37 +02:00
|
|
|
|
from reportlab.lib.units import inch, cm, mm
|
|
|
|
|
from reportlab.lib import styles
|
2022-03-24 09:59:44 +01:00
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
2021-08-21 15:17:14 +02:00
|
|
|
|
from flask import g
|
|
|
|
|
|
2023-08-28 17:02:21 +02:00
|
|
|
|
from app import log
|
|
|
|
|
from app.scodoc.sco_exceptions import ScoGenError, ScoPDFFormatError, ScoValueError
|
2022-07-08 18:09:45 +02:00
|
|
|
|
from app.scodoc import sco_utils as scu
|
2021-11-07 09:07:51 +01:00
|
|
|
|
from app.scodoc.sco_utils import CONFIG
|
2021-08-21 17:07:44 +02:00
|
|
|
|
import sco_version
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
|
PAGE_HEIGHT = defaultPageSize[1]
|
|
|
|
|
PAGE_WIDTH = defaultPageSize[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_PDF_FOOTER_TEMPLATE = CONFIG.DEFAULT_PDF_FOOTER_TEMPLATE
|
|
|
|
|
|
|
|
|
|
|
2022-07-08 18:09:45 +02:00
|
|
|
|
def SU(s: str) -> str:
|
2021-07-13 17:00:25 +02:00
|
|
|
|
"convert s from string to string suitable for ReportLab"
|
2021-07-25 22:31:59 +02:00
|
|
|
|
if not s:
|
|
|
|
|
return ""
|
|
|
|
|
else:
|
|
|
|
|
# Remplace caractères composés
|
|
|
|
|
# eg 'e\xcc\x81' COMBINING ACUTE ACCENT par '\xc3\xa9' LATIN SMALL LETTER E WITH ACUTE
|
|
|
|
|
# car les "combining accents" ne sont pas traités par ReportLab mais peuvent
|
|
|
|
|
# nous être envoyés par certains navigateurs ou imports
|
|
|
|
|
# (on en a dans les bases de données)
|
2021-09-07 23:54:33 +02:00
|
|
|
|
s = unicodedata.normalize("NFC", s)
|
2022-10-28 23:04:44 +02:00
|
|
|
|
# Remplace les entités XML/HTML
|
2021-09-07 23:54:33 +02:00
|
|
|
|
# reportlab ne les supporte pas non plus.
|
|
|
|
|
s = html.unescape(s)
|
2022-10-28 23:04:44 +02:00
|
|
|
|
# Remplace les <br> par des <br/>
|
|
|
|
|
s = re.sub(r"<br\s*>", "<br/>", s)
|
2023-07-04 22:54:55 +02:00
|
|
|
|
# And substitute unicode characters not supported by ReportLab
|
|
|
|
|
s = s.replace("‐", "-")
|
2021-09-07 23:54:33 +02:00
|
|
|
|
return s
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
2022-03-24 09:59:44 +01:00
|
|
|
|
def get_available_font_names() -> list[str]:
|
|
|
|
|
"""List installed font names"""
|
|
|
|
|
can = canvas.Canvas(io.StringIO())
|
|
|
|
|
return can.getAvailableFonts()
|
|
|
|
|
|
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
def _splitPara(txt):
|
|
|
|
|
"split a string, returns a list of <para > ... </para>"
|
|
|
|
|
L = []
|
|
|
|
|
closetag = "</para>"
|
|
|
|
|
l = len(closetag)
|
|
|
|
|
start = 0
|
|
|
|
|
e = -1
|
|
|
|
|
while 1:
|
|
|
|
|
b = txt.find("<para", start)
|
|
|
|
|
if b < 0:
|
|
|
|
|
if e < 0:
|
|
|
|
|
L.append(txt) # no para, just return text
|
|
|
|
|
break
|
|
|
|
|
e = txt.find(closetag, b)
|
|
|
|
|
if e < 0:
|
|
|
|
|
raise ValueError("unbalanced para tags")
|
|
|
|
|
L.append(txt[b : e + l])
|
|
|
|
|
start = e
|
|
|
|
|
# fix para: must be followed by a newline (? Reportlab bug turnaround ?)
|
|
|
|
|
L = [re.sub("<para(.*?)>([^\n\r])", "<para\\1>\n\\2", p) for p in L]
|
|
|
|
|
|
|
|
|
|
return L
|
|
|
|
|
|
|
|
|
|
|
2022-11-20 23:03:26 +01:00
|
|
|
|
def make_paras(txt: str, style, suppress_empty=False) -> list[Paragraph]:
|
2020-09-26 16:19:37 +02:00
|
|
|
|
"""Returns a list of Paragraph instances from a text
|
|
|
|
|
with one or more <para> ... </para>
|
|
|
|
|
"""
|
2021-10-04 00:22:44 +02:00
|
|
|
|
result = []
|
2020-09-26 16:19:37 +02:00
|
|
|
|
try:
|
|
|
|
|
paras = _splitPara(txt)
|
|
|
|
|
if suppress_empty:
|
|
|
|
|
r = []
|
|
|
|
|
for para in paras:
|
2021-01-04 13:53:19 +01:00
|
|
|
|
m = re.match(r"\s*<\s*para.*>\s*(.*)\s*<\s*/\s*para\s*>\s*", para)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
if not m:
|
|
|
|
|
r.append(para) # not a paragraph, keep it
|
|
|
|
|
else:
|
|
|
|
|
if m.group(1): # non empty paragraph
|
|
|
|
|
r.append(para)
|
|
|
|
|
paras = r
|
2023-08-28 17:02:21 +02:00
|
|
|
|
try:
|
|
|
|
|
result = [Paragraph(SU(s), style) for s in paras]
|
|
|
|
|
except AttributeError as exc:
|
|
|
|
|
raise ScoPDFFormatError("PDF: paragraphe avec balisage invalide") from exc
|
2021-10-04 00:22:44 +02:00
|
|
|
|
except OSError as e:
|
|
|
|
|
msg = str(e)
|
|
|
|
|
# If a file is missing, try to display the invalid name
|
|
|
|
|
m = re.match(r".*\sfilename=\'(.*?)\'.*", msg, re.DOTALL)
|
|
|
|
|
if m:
|
|
|
|
|
filename = os.path.split(m.group(1))[1]
|
|
|
|
|
if filename.startswith("logo_"):
|
|
|
|
|
filename = filename[len("logo_") :]
|
|
|
|
|
raise ScoValueError(
|
|
|
|
|
f"Erreur dans le format PDF paramétré: fichier logo <b>{filename}</b> non trouvé"
|
|
|
|
|
) from e
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2022-07-08 18:09:45 +02:00
|
|
|
|
except Exception as exc:
|
2020-09-26 16:19:37 +02:00
|
|
|
|
log(traceback.format_exc())
|
2022-07-08 18:09:45 +02:00
|
|
|
|
log(f"Invalid pdf para format: {txt}")
|
2022-03-24 09:59:44 +01:00
|
|
|
|
try:
|
|
|
|
|
result = [
|
|
|
|
|
Paragraph(
|
|
|
|
|
SU('<font color="red"><i>Erreur: format invalide</i></font>'),
|
|
|
|
|
style,
|
|
|
|
|
)
|
|
|
|
|
]
|
2022-11-20 23:03:26 +01:00
|
|
|
|
except ValueError: # probleme font ? essaye sans style
|
|
|
|
|
# récupère font en cause ?
|
|
|
|
|
m = re.match(r".*family/bold/italic for (.*)", exc.args[0], re.DOTALL)
|
2022-03-24 09:59:44 +01:00
|
|
|
|
if m:
|
|
|
|
|
message = f"police non disponible: {m[1]}"
|
|
|
|
|
else:
|
|
|
|
|
message = "format invalide"
|
2022-07-08 18:09:45 +02:00
|
|
|
|
scu.flash_once(f"problème génération PDF: {message}")
|
2022-03-24 09:59:44 +01:00
|
|
|
|
return [
|
|
|
|
|
Paragraph(
|
|
|
|
|
SU(f'<font color="red"><b>Erreur: {message}</b></font>'),
|
|
|
|
|
reportlab.lib.styles.ParagraphStyle({}),
|
|
|
|
|
)
|
|
|
|
|
]
|
2021-10-04 00:22:44 +02:00
|
|
|
|
return result
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bold_paras(L, tag="b", close=None):
|
|
|
|
|
"""Put each (string) element of L between <b>
|
|
|
|
|
L is a dict or sequence. (if dict, elements with key begining by _ are unaffected)
|
|
|
|
|
"""
|
|
|
|
|
b = "<" + tag + ">"
|
|
|
|
|
if not close:
|
|
|
|
|
close = "</" + tag + ">"
|
|
|
|
|
if hasattr(L, "keys"):
|
|
|
|
|
# L is a dict
|
|
|
|
|
for k in L:
|
|
|
|
|
if k[0] != "_":
|
|
|
|
|
L[k] = b + L[k] or "" + close
|
|
|
|
|
return L
|
|
|
|
|
else:
|
|
|
|
|
# L is a sequence
|
|
|
|
|
return [b + (x or "") + close for x in L]
|
|
|
|
|
|
|
|
|
|
|
2022-03-12 09:40:48 +01:00
|
|
|
|
class BulMarker(Flowable):
|
|
|
|
|
"""Custom Flowables pour nos bulletins PDF: invisibles, juste pour se repérer"""
|
|
|
|
|
|
|
|
|
|
def wrap(self, *args):
|
|
|
|
|
return (0, 0)
|
|
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DebutBulletin(BulMarker):
|
|
|
|
|
"""Début d'un bulletin.
|
|
|
|
|
Element vide utilisé pour générer les bookmarks
|
|
|
|
|
"""
|
|
|
|
|
|
2022-03-13 23:00:50 +01:00
|
|
|
|
def __init__(self, bookmark=None, filigranne=None, footer_content=None):
|
2022-03-12 09:40:48 +01:00
|
|
|
|
self.bookmark = bookmark
|
2022-03-13 22:02:30 +01:00
|
|
|
|
self.filigranne = filigranne
|
2022-03-13 23:00:50 +01:00
|
|
|
|
self.footer_content = footer_content
|
2022-03-12 09:40:48 +01:00
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScoDocPageTemplate(PageTemplate):
|
2020-09-26 16:19:37 +02:00
|
|
|
|
"""Our own page template."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
document,
|
2023-02-19 15:45:27 +01:00
|
|
|
|
pagesbookmarks: dict = None,
|
2020-09-26 16:19:37 +02:00
|
|
|
|
author=None,
|
|
|
|
|
title=None,
|
|
|
|
|
subject=None,
|
|
|
|
|
margins=(0, 0, 0, 0), # additional margins in mm (left,top,right, bottom)
|
|
|
|
|
server_name="",
|
|
|
|
|
footer_template=DEFAULT_PDF_FOOTER_TEMPLATE,
|
|
|
|
|
filigranne=None,
|
|
|
|
|
preferences=None, # dictionnary with preferences, required
|
|
|
|
|
):
|
|
|
|
|
"""Initialise our page template."""
|
2022-03-13 22:02:30 +01:00
|
|
|
|
# defered import (solve circular dependency ->sco_logo ->scodoc, ->sco_pdf
|
|
|
|
|
from app.scodoc.sco_logos import find_logo
|
2021-11-19 11:51:05 +01:00
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.preferences = preferences
|
2022-03-13 22:02:30 +01:00
|
|
|
|
self.pagesbookmarks = pagesbookmarks or {}
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.pdfmeta_author = author
|
|
|
|
|
self.pdfmeta_title = title
|
|
|
|
|
self.pdfmeta_subject = subject
|
|
|
|
|
self.server_name = server_name
|
|
|
|
|
self.filigranne = filigranne
|
2022-03-12 09:40:48 +01:00
|
|
|
|
self.page_number = 1
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.footer_template = footer_template
|
2021-07-25 22:31:59 +02:00
|
|
|
|
if self.preferences:
|
|
|
|
|
self.with_page_background = self.preferences["bul_pdf_with_background"]
|
|
|
|
|
else:
|
|
|
|
|
self.with_page_background = False
|
2020-10-21 22:56:25 +02:00
|
|
|
|
self.background_image_filename = None
|
2020-09-26 16:19:37 +02:00
|
|
|
|
# Our doc is made of a single frame
|
|
|
|
|
left, top, right, bottom = [float(x) for x in margins]
|
|
|
|
|
content = Frame(
|
|
|
|
|
10.0 * mm + left * mm,
|
|
|
|
|
13.0 * mm + bottom * mm,
|
|
|
|
|
document.pagesize[0] - 20.0 * mm - left * mm - right * mm,
|
|
|
|
|
document.pagesize[1] - 18.0 * mm - top * mm - bottom * mm,
|
|
|
|
|
)
|
2022-03-12 09:40:48 +01:00
|
|
|
|
super().__init__("ScoDocPageTemplate", [content])
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.logo = None
|
2021-11-07 09:07:51 +01:00
|
|
|
|
logo = find_logo(
|
2022-02-26 11:00:08 +01:00
|
|
|
|
logoname="bul_pdf_background", dept_id=g.scodoc_dept_id
|
|
|
|
|
) or find_logo(
|
|
|
|
|
logoname="bul_pdf_background", dept_id=g.scodoc_dept_id, prefix=""
|
2021-11-07 09:07:51 +01:00
|
|
|
|
)
|
|
|
|
|
if logo is None:
|
|
|
|
|
# Also try to use PV background
|
|
|
|
|
logo = find_logo(
|
2022-02-26 11:00:08 +01:00
|
|
|
|
logoname="letter_background", dept_id=g.scodoc_dept_id
|
|
|
|
|
) or find_logo(
|
|
|
|
|
logoname="letter_background", dept_id=g.scodoc_dept_id, prefix=""
|
2021-11-07 09:07:51 +01:00
|
|
|
|
)
|
|
|
|
|
if logo is not None:
|
|
|
|
|
self.background_image_filename = logo.filepath
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
2022-03-24 09:59:44 +01:00
|
|
|
|
def beforeDrawPage(self, canv, doc):
|
2020-10-21 22:56:25 +02:00
|
|
|
|
"""Draws (optional) background, logo and contribution message on each page.
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
|
day : Day of the month as a decimal number [01,31]
|
|
|
|
|
month : Month as a decimal number [01,12].
|
|
|
|
|
year : Year without century as a decimal number [00,99].
|
|
|
|
|
Year : Year with century as a decimal number.
|
|
|
|
|
hour : Hour (24-hour clock) as a decimal number [00,23].
|
|
|
|
|
minute: Minute as a decimal number [00,59].
|
|
|
|
|
|
|
|
|
|
server_url: URL du serveur ScoDoc
|
2020-10-13 15:41:04 +02:00
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
"""
|
2021-07-25 22:31:59 +02:00
|
|
|
|
if not self.preferences:
|
|
|
|
|
return
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.saveState()
|
2020-10-21 22:56:25 +02:00
|
|
|
|
# ---- Background image
|
|
|
|
|
if self.background_image_filename and self.with_page_background:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.drawImage(
|
2020-10-21 22:56:25 +02:00
|
|
|
|
self.background_image_filename, 0, 0, doc.pagesize[0], doc.pagesize[1]
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-13 15:41:04 +02:00
|
|
|
|
# ---- Logo: a small image, positionned at top left of the page
|
2020-09-26 16:19:37 +02:00
|
|
|
|
if self.logo is not None:
|
|
|
|
|
# draws the logo if it exists
|
2021-01-04 13:53:19 +01:00
|
|
|
|
( # pylint: disable=unpacking-non-sequence
|
|
|
|
|
(width, height),
|
|
|
|
|
image,
|
|
|
|
|
) = self.logo
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.drawImage(image, inch, doc.pagesize[1] - inch, width, height)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
|
# ---- Add some meta data and bookmarks
|
|
|
|
|
if self.pdfmeta_author:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.setAuthor(SU(self.pdfmeta_author))
|
2020-09-26 16:19:37 +02:00
|
|
|
|
if self.pdfmeta_title:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.setTitle(SU(self.pdfmeta_title))
|
2020-09-26 16:19:37 +02:00
|
|
|
|
if self.pdfmeta_subject:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.setSubject(SU(self.pdfmeta_subject))
|
2022-03-13 22:02:30 +01:00
|
|
|
|
|
|
|
|
|
bookmark = self.pagesbookmarks.get(doc.page, None)
|
|
|
|
|
if bookmark:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.bookmarkPage(bookmark)
|
|
|
|
|
canv.addOutlineEntry(SU(bookmark), bookmark)
|
2022-03-13 23:00:50 +01:00
|
|
|
|
|
2022-03-24 09:59:44 +01:00
|
|
|
|
def draw_footer(self, canv, content):
|
2022-03-13 23:00:50 +01:00
|
|
|
|
"""Print the footer"""
|
2023-02-14 17:28:25 +01:00
|
|
|
|
try:
|
|
|
|
|
canv.setFont(
|
|
|
|
|
self.preferences["SCOLAR_FONT"],
|
|
|
|
|
self.preferences["SCOLAR_FONT_SIZE_FOOT"],
|
|
|
|
|
)
|
|
|
|
|
except KeyError as exc:
|
|
|
|
|
raise ScoValueError(
|
|
|
|
|
f"""Police invalide dans pied de page pdf: {
|
|
|
|
|
self.preferences['SCOLAR_FONT']
|
|
|
|
|
} (taille {self.preferences['SCOLAR_FONT_SIZE_FOOT']})"""
|
|
|
|
|
) from exc
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.drawString(
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.preferences["pdf_footer_x"] * mm,
|
|
|
|
|
self.preferences["pdf_footer_y"] * mm,
|
2022-03-13 23:00:50 +01:00
|
|
|
|
content,
|
2020-09-26 16:19:37 +02:00
|
|
|
|
)
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.restoreState()
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
2022-03-13 23:00:50 +01:00
|
|
|
|
def footer_string(self) -> str:
|
|
|
|
|
"""String contenu du pied de page"""
|
2023-08-30 16:03:36 +02:00
|
|
|
|
d = _make_datetime_dict()
|
2022-03-13 23:00:50 +01:00
|
|
|
|
d["scodoc_name"] = sco_version.SCONAME
|
|
|
|
|
d["server_url"] = self.server_name
|
|
|
|
|
return SU(self.footer_template % d)
|
|
|
|
|
|
2022-03-24 09:59:44 +01:00
|
|
|
|
def afterDrawPage(self, canv, doc):
|
2022-03-12 09:40:48 +01:00
|
|
|
|
if not self.preferences:
|
|
|
|
|
return
|
2022-03-13 23:00:50 +01:00
|
|
|
|
# ---- Footer
|
|
|
|
|
foot_content = None
|
|
|
|
|
if hasattr(doc, "current_footer"):
|
|
|
|
|
foot_content = doc.current_footer
|
2022-03-24 09:59:44 +01:00
|
|
|
|
self.draw_footer(canv, foot_content or self.footer_string())
|
2022-03-12 09:40:48 +01:00
|
|
|
|
# ---- Filigranne (texte en diagonal en haut a gauche de chaque page)
|
2022-03-13 22:02:30 +01:00
|
|
|
|
filigranne = None
|
|
|
|
|
if hasattr(doc, "filigranne"):
|
|
|
|
|
# filigranne crée par DebutBulletin
|
|
|
|
|
filigranne = doc.filigranne
|
|
|
|
|
if not filigranne and self.filigranne:
|
2022-03-12 09:40:48 +01:00
|
|
|
|
if isinstance(self.filigranne, str):
|
|
|
|
|
filigranne = self.filigranne # same for all pages
|
|
|
|
|
else:
|
|
|
|
|
filigranne = self.filigranne.get(doc.page, None)
|
2022-03-13 22:02:30 +01:00
|
|
|
|
if filigranne:
|
2022-03-24 09:59:44 +01:00
|
|
|
|
canv.saveState()
|
|
|
|
|
canv.translate(9 * cm, 27.6 * cm)
|
|
|
|
|
canv.rotate(30)
|
|
|
|
|
canv.scale(4.5, 4.5)
|
|
|
|
|
canv.setFillColorRGB(1.0, 0.65, 0.65, alpha=0.6)
|
|
|
|
|
canv.drawRightString(0, 0, SU(filigranne))
|
|
|
|
|
canv.restoreState()
|
2022-03-13 22:02:30 +01:00
|
|
|
|
doc.filigranne = None
|
2022-03-12 09:40:48 +01:00
|
|
|
|
|
|
|
|
|
def afterPage(self):
|
|
|
|
|
"""Called after all flowables have been drawn on a page.
|
|
|
|
|
Increment pageNum since the page has been completed.
|
|
|
|
|
"""
|
|
|
|
|
self.page_number += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulletinDocTemplate(BaseDocTemplate):
|
|
|
|
|
"""Doc template pour les bulletins PDF
|
|
|
|
|
ajoute la gestion des bookmarks
|
|
|
|
|
"""
|
|
|
|
|
|
2023-02-19 15:45:27 +01:00
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
|
super().__init__(*args, **kw)
|
|
|
|
|
self.current_footer = ""
|
|
|
|
|
self.filigranne = None
|
|
|
|
|
|
2022-03-12 09:40:48 +01:00
|
|
|
|
# inspired by https://www.reportlab.com/snippets/13/
|
|
|
|
|
def afterFlowable(self, flowable):
|
|
|
|
|
"""Called by Reportlab after each flowable"""
|
|
|
|
|
if isinstance(flowable, DebutBulletin):
|
2022-03-13 23:00:50 +01:00
|
|
|
|
self.current_footer = ""
|
2022-03-12 09:40:48 +01:00
|
|
|
|
if flowable.bookmark:
|
2022-03-13 23:00:50 +01:00
|
|
|
|
self.current_footer = flowable.footer_content
|
2022-03-12 09:40:48 +01:00
|
|
|
|
self.canv.bookmarkPage(flowable.bookmark)
|
|
|
|
|
self.canv.addOutlineEntry(
|
|
|
|
|
SU(flowable.bookmark), flowable.bookmark, level=0, closed=None
|
|
|
|
|
)
|
2022-03-13 22:02:30 +01:00
|
|
|
|
if flowable.filigranne:
|
|
|
|
|
self.filigranne = flowable.filigranne
|
2022-03-12 09:40:48 +01:00
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
2023-08-30 16:03:36 +02:00
|
|
|
|
def _make_datetime_dict() -> dict:
|
|
|
|
|
"a dict with date elements for templates"
|
|
|
|
|
now = datetime.datetime.now()
|
2020-09-26 16:19:37 +02:00
|
|
|
|
return {
|
2023-08-30 16:03:36 +02:00
|
|
|
|
"day": now.day,
|
|
|
|
|
"month": now.month,
|
|
|
|
|
"year": now.year,
|
|
|
|
|
"Year": now.year,
|
|
|
|
|
"hour": now.hour,
|
|
|
|
|
"minute": now.minute,
|
2020-09-26 16:19:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pdf_basic_page(
|
2021-08-19 10:28:35 +02:00
|
|
|
|
objects,
|
|
|
|
|
title="",
|
|
|
|
|
preferences=None,
|
2020-09-26 16:19:37 +02:00
|
|
|
|
): # used by gen_table.make_page()
|
|
|
|
|
"""Simple convenience fonction: build a page from a list of platypus objects,
|
|
|
|
|
adding a title if specified.
|
|
|
|
|
"""
|
|
|
|
|
StyleSheet = styles.getSampleStyleSheet()
|
2021-07-25 22:31:59 +02:00
|
|
|
|
report = io.BytesIO() # in-memory document, no disk file
|
2020-09-26 16:19:37 +02:00
|
|
|
|
document = BaseDocTemplate(report)
|
|
|
|
|
document.addPageTemplates(
|
2022-03-12 09:40:48 +01:00
|
|
|
|
ScoDocPageTemplate(
|
2020-09-26 16:19:37 +02:00
|
|
|
|
document,
|
|
|
|
|
title=title,
|
2021-08-21 17:07:44 +02:00
|
|
|
|
author="%s %s (E. Viennet)" % (sco_version.SCONAME, sco_version.SCOVERSION),
|
2020-09-26 16:19:37 +02:00
|
|
|
|
footer_template="Edité par %(scodoc_name)s le %(day)s/%(month)s/%(year)s à %(hour)sh%(minute)s",
|
|
|
|
|
preferences=preferences,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if title:
|
|
|
|
|
head = Paragraph(SU(title), StyleSheet["Heading3"])
|
|
|
|
|
objects = [head] + objects
|
|
|
|
|
document.build(objects)
|
|
|
|
|
data = report.getvalue()
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Gestion du lock pdf
|
|
|
|
|
|
|
|
|
|
|
2021-07-09 23:31:16 +02:00
|
|
|
|
class PDFLock(object):
|
2020-09-26 16:19:37 +02:00
|
|
|
|
def __init__(self, timeout=15):
|
2021-08-22 07:47:06 +02:00
|
|
|
|
self.Q = queue.Queue(1)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.timeout = timeout
|
|
|
|
|
self.current_thread = None
|
|
|
|
|
self.nref = 0
|
|
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
|
"Release lock. Raise Empty if not acquired first"
|
2021-08-22 07:47:06 +02:00
|
|
|
|
if self.current_thread == threading.get_ident():
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.nref -= 1
|
|
|
|
|
if self.nref == 0:
|
|
|
|
|
log("PDFLock: release from %s" % self.current_thread)
|
|
|
|
|
self.current_thread = None
|
|
|
|
|
self.Q.get(False)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
self.Q.get(False)
|
|
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
|
"Acquire lock. Raise ScoGenError if can't lock after timeout."
|
2021-08-22 07:47:06 +02:00
|
|
|
|
if self.current_thread == threading.get_ident():
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.nref += 1
|
|
|
|
|
return # deja lock pour ce thread
|
|
|
|
|
try:
|
|
|
|
|
self.Q.put(1, True, self.timeout)
|
2022-03-24 09:59:44 +01:00
|
|
|
|
except queue.Full as e:
|
|
|
|
|
raise ScoGenError(msg="Traitement PDF occupé: ré-essayez") from e
|
2021-08-22 07:47:06 +02:00
|
|
|
|
self.current_thread = threading.get_ident()
|
2020-09-26 16:19:37 +02:00
|
|
|
|
self.nref = 1
|
|
|
|
|
log("PDFLock: granted to %s" % self.current_thread)
|
|
|
|
|
|
|
|
|
|
|
2021-08-22 15:36:17 +02:00
|
|
|
|
class WatchLock:
|
|
|
|
|
"Surveille threads (mais ne verrouille pas)"
|
|
|
|
|
|
|
|
|
|
def __init__(self, timeout=None):
|
|
|
|
|
self.timeout = timeout
|
|
|
|
|
t = threading.current_thread()
|
|
|
|
|
self.native_id = t.native_id
|
|
|
|
|
self.ident = t.ident
|
|
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
|
t = threading.current_thread()
|
|
|
|
|
if (self.native_id != t.native_id) or (self.ident != t.ident):
|
|
|
|
|
log(
|
|
|
|
|
f"Warning: LOCK detected several threads ! (native_id {self.native_id} -> {t.native_id}, ident {self.ident} -> {t.ident}"
|
|
|
|
|
)
|
|
|
|
|
self.native_id = t.native_id
|
|
|
|
|
self.ident = t.ident
|
|
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
|
t = threading.current_thread()
|
|
|
|
|
assert (self.native_id == t.native_id) and (self.ident == t.ident)
|
|
|
|
|
|
|
|
|
|
|
2021-08-22 07:47:06 +02:00
|
|
|
|
class FakeLock:
|
|
|
|
|
"Pour ScoDoc 9: pas de verrou"
|
|
|
|
|
|
2021-08-22 15:36:17 +02:00
|
|
|
|
def __init__(self, timeout=None):
|
|
|
|
|
pass
|
2021-08-22 07:47:06 +02:00
|
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PDFLOCK = FakeLock()
|