Skip to content

Commit

Permalink
Fix use of downloaded fonts for pptx build type
Browse files Browse the repository at this point in the history
imgkit runs in /tmp, so we need to either copy the fonts there for resolve all the paths

Set log-level on imgkit to warn because missing font files is a warning, not an error
  • Loading branch information
TylerZeroMaster committed Aug 22, 2024
1 parent cebb3b8 commit f4c1fbb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
10 changes: 4 additions & 6 deletions bakery-src/scripts/pptify_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,13 @@ def auto_crop_img(img, bg=None):
# due to some kind of rate limiting. If this becomes a problem, a possible
# workaround is to download the font css once and update the references in the
# pdf css so that the font is read from the local copy
def xhtml_to_img(elem, *, css=[], resource_dir=None):
def xhtml_to_img(elem, *, css=[]):
options = {
"format": "png",
"log-level": "error",
"log-level": "warn",
"quality": "100",
"enable-local-file-access": "",
}
if resource_dir is not None:
options["enable-local-file-access"] = ""
options["allow"] = resource_dir
with BytesIO() as img_file:
serialized = etree.tostring(elem, encoding="unicode")
assert serialized, "Cannot convert empty element"
Expand Down Expand Up @@ -511,7 +509,7 @@ def element_to_image(
rel_p = resource.get(attr)
abs_p = (doc_dir / rel_p).resolve()
resource.set(attr, str(abs_p))
img = xhtml_to_img(elem, resource_dir=resource_dir, css=css)
img = xhtml_to_img(elem, css=css)
img = auto_crop_img(img)
return img

Expand Down
15 changes: 7 additions & 8 deletions bakery-src/tests/test_bakery_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4598,24 +4598,24 @@ def test_ppt_image_transforms(mocker):
css=[],
options={
"format": "png",
"log-level": "error",
"log-level": "warn",
"quality": "100",
"enable-local-file-access": "",
},
)
assert result.size == img.size

imgkit_from_string_stub.reset_mock()
result = pptify_book.xhtml_to_img(xhtml, resource_dir="test")
result = pptify_book.xhtml_to_img(xhtml)
imgkit_from_string_stub.assert_called_once_with(
etree.tostring(xhtml, encoding="unicode"),
None,
css=[],
options={
"allow": "test",
"enable-local-file-access": "",
"format": "png",
"log-level": "error",
"log-level": "warn",
"quality": "100",
"enable-local-file-access": "",
},
)
assert result.size == img.size
Expand Down Expand Up @@ -4652,11 +4652,10 @@ def get_doc_dir(self):
None,
css=[],
options={
"allow": resource_dir,
"enable-local-file-access": "",
"format": "png",
"log-level": "error",
"log-level": "warn",
"quality": "100",
"enable-local-file-access": "",
},
)
mocker.stopall()
Expand Down
1 change: 1 addition & 0 deletions dockerfiles/steps/step-pptx.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parse_book_dir

set -Eeuo pipefail

cp -R "$IO_BAKED/downloaded-fonts" "/tmp"
mathml2png_rpc start
for book in "$IO_LINKED/"*.xhtml; do
lang=en
Expand Down

0 comments on commit f4c1fbb

Please sign in to comment.