Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Reading custom Font *.ttf from Java class
So, I'm trying to load a custom *.ttf from my Java class within a Liferay MVC portlet. My file is located in /docroot/fonts/arial.ttf and I'm trying to load it with the following code
String path = httpRequest.getContextPath() + "/fonts/arial.ttf";
String filePath = FileUtil.getAbsolutePath(new File(path));
System.out.println("\n -- Does File: " + filePath + " Exist? " + FileUtil.exists(filePath) + "-- \n");
My output from the System.out.println with the "exists" method is false
-- Does File: /compass-dashboard-roster-portlet/fonts/arial.ttf Exist? false--
I'm trying to use the custom font *.ttf in a jar that builds a PDF using iText 7. How do I load this file from the portlet? See attached for directory structure
String path = httpRequest.getContextPath() + "/fonts/arial.ttf";
String filePath = FileUtil.getAbsolutePath(new File(path));
System.out.println("\n -- Does File: " + filePath + " Exist? " + FileUtil.exists(filePath) + "-- \n");
My output from the System.out.println with the "exists" method is false
-- Does File: /compass-dashboard-roster-portlet/fonts/arial.ttf Exist? false--
I'm trying to use the custom font *.ttf in a jar that builds a PDF using iText 7. How do I load this file from the portlet? See attached for directory structure
Attachments:
Kevin Neibarger:
It depends a bit on which version you're on, however, you can't rely on anything deployed with your web application to ever be addressable as File on disk: It might be packaged in a zip, jar or war, just in memory. Thus you'll always need to find out how to open a stream and read the data from it.
-- Does File: /compass-dashboard-roster-portlet/fonts/arial.ttf Exist? false--
Even if you would find it on disk in your current case, you can't rely on it in the future. And if you'd ignore this, your way to construct the filename is most likely wrong: If you read the message carefully: Does your filesystem have a top level directory /compass-dashboard-roster-portlet? I'm guessing it doesn't.
I figured out that I needed to move the custom fonts to the jar I was importing into my portlet and make it available to my jar executable. I am using the jar library to build my form so I don't need dependencies in my portlet with the exception of said jar.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™