Message Boards
Reading custom Font *.ttf from Java class
Kevin Neibarger, modified 4 Years ago.
Reading custom Font *.ttf from Java class
Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
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:
Olaf Kock, modified 4 Years ago.
RE: Reading custom Font *.ttf from Java class
Liferay Legend Posts: 6396 Join Date: 9/23/08 Recent PostsKevin 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.
Kevin Neibarger, modified 4 Years ago.
RE: Reading custom Font *.ttf from Java class
Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
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.