From 7cefdca7ac53e6a4962afc8722e03cde222caac0 Mon Sep 17 00:00:00 2001 From: Tiago Salvado Date: Wed, 15 Nov 2023 15:48:05 +0000 Subject: [PATCH] [MNT-23960] Changed the way how font is loaded --- .../TextToPdfContentTransformer.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java index afa910ec8..319a1f471 100644 --- a/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java +++ b/engines/misc/src/main/java/org/alfresco/transform/misc/transformers/TextToPdfContentTransformer.java @@ -399,6 +399,7 @@ public void setFont(String aFontName) private PDFont getFont(PDDocument doc, String name) { PDFont font = null; + InputStream fontIS = null; if (name == null && !fontChanged) { @@ -410,33 +411,38 @@ private PDFont getFont(PDDocument doc, String name) if (name != null) { String location = "fonts" + System.getProperty("file.separator") + name + ".ttf"; - ClassLoader loader = TextToPdfContentTransformer.class.getClassLoader(); - File fontFile = null; + ClassLoader loader = PagedTextToPDF.class.getClassLoader(); if (null != loader) { - URL resource = loader.getResource(location); - if (resource != null) - { - String file = resource.getFile(); - if (file != null && !file.isEmpty()) - { - fontFile = new File(file); - } - } + fontIS = loader.getResourceAsStream(location); } - if (null != fontFile) + if (null != fontIS) { PDDocument documentMock = new PDDocument(); - font = PDType0Font.load(documentMock, fontFile); + font = PDType0Font.load(documentMock, fontIS); } } } - catch (IOException e) + catch (IOException ioe) + { + String msg = "Error loading font " + name + " :" + ioe.getMessage(); + logger.error(msg, ioe); + } + finally { - String msg = "Error loading font " + name + " :" + e.getMessage(); - logger.error(msg, e); + if (fontIS != null) + { + try + { + fontIS.close(); + } + catch (Exception e) + { + logger.error("Error closing font inputstream: " + e.getMessage()); + } + } } if (font == null)