Skip to content

Commit

Permalink
[MNT-23960] Changed the way how font is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosalvado10 committed Nov 15, 2023
1 parent c6ff737 commit 7cefdca
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down

0 comments on commit 7cefdca

Please sign in to comment.