Skip to content

Commit b369d98

Browse files
committed
fix(docworker): Fix WeasyPrint and FontTools config
1 parent 6c1a2e5 commit b369d98

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

.cspell/dictionary.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ thechapter
8888
setul
8989
setulcolor
9090
bbding
91+
fontconfig
9192

9293
# Project specific terms
9394
docworker
@@ -118,5 +119,3 @@ odend
118119
heighta
119120
xurl
120121
nosniff
121-
122-
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pathlib
3+
import tempfile
34

45
from . import consts
56
from .cli import load_config_str
@@ -10,10 +11,16 @@ def lambda_handler(event, context):
1011
config_path = pathlib.Path(os.getenv(consts.VAR_APP_CONFIG_PATH, '/var/task/application.yml'))
1112
workdir_path = pathlib.Path(os.getenv(consts.VAR_WORKDIR_PATH, '/var/task/templates'))
1213

13-
config = load_config_str(config_path.read_text(encoding=consts.DEFAULT_ENCODING))
14-
try:
15-
doc_worker = DocumentWorker(config, workdir_path)
16-
doc_worker.run_once()
17-
except Exception as e:
18-
SentryReporter.capture_exception(e)
19-
raise e
14+
with tempfile.TemporaryDirectory() as tmpdir:
15+
cache_dir = pathlib.Path(tmpdir) / '.cache'
16+
fontconfig_tmp = cache_dir / 'fontconfig'
17+
os.environ['XDG_CACHE_HOME'] = cache_dir.as_posix()
18+
fontconfig_tmp.mkdir(parents=True, exist_ok=True)
19+
20+
config = load_config_str(config_path.read_text(encoding=consts.DEFAULT_ENCODING))
21+
try:
22+
doc_worker = DocumentWorker(config, workdir_path)
23+
doc_worker.run_once()
24+
except Exception as e:
25+
SentryReporter.capture_exception(e)
26+
raise

packages/dsw-document-worker/dsw/document_worker/templates/steps/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def __init__(self, template, options: dict[str, str]):
2121
extras_str: str = self.options.get(self.OPTION_EXTRAS, '')
2222
self.extras: set[str] = set(extras_str.split(','))
2323

24+
@staticmethod
25+
def initialize_step():
26+
pass
27+
2428
def requires_via_extras(self, requirement: str) -> bool:
2529
return requirement in self.extras
2630

@@ -38,6 +42,7 @@ def raise_exc(self, message: str):
3842

3943

4044
def register_step(name: str, step_class: type[Step]):
45+
step_class.initialize_step()
4146
STEPS[name.lower()] = step_class
4247

4348

packages/dsw-document-worker/dsw/document_worker/templates/steps/conversion.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import logging
2+
3+
import weasyprint
4+
15
from ... import consts
26
from ...context import Context
37
from ...conversions import Pandoc, RdfLibConvert
@@ -15,14 +19,21 @@ class WeasyPrintStep(Step):
1519
OUTPUT_FORMAT = FileFormats.PDF
1620

1721
def __init__(self, template, options: dict):
18-
import weasyprint
19-
2022
super().__init__(template, options)
2123
# PDF options
22-
self.wp_options = weasyprint.DEFAULT_OPTIONS
24+
self.wp_options = weasyprint.DEFAULT_OPTIONS.copy()
2325
self.wp_update_options(options)
2426
self.wp_zoom = float(options.get('pdf.zoom', '1'))
2527

28+
@staticmethod
29+
def initialize_step():
30+
# mute weasyprint logger
31+
logging.getLogger('weasyprint').setLevel(logging.WARNING)
32+
# mute fontTools loggers
33+
for logger_name in logging.root.manager.loggerDict:
34+
if logger_name.startswith('fontTools'):
35+
logging.getLogger(logger_name).setLevel(logging.WARNING)
36+
2637
def wp_update_options(self, options: dict):
2738
optimize_size = tuple(options.get('render.optimize_size', 'fonts').split(','))
2839
self.wp_options.update({
@@ -57,7 +68,7 @@ def execute_follow(self, document: DocumentFile, context: dict) -> DocumentFile:
5768
zoom=self.wp_zoom,
5869
font_config=None, # not used now (should be in CSS)
5970
counter_style=None, # not used now (should be in CSS)
60-
options=self.options,
71+
**self.wp_options,
6172
)
6273
return DocumentFile(
6374
file_format=self.OUTPUT_FORMAT,

0 commit comments

Comments
 (0)