I have the following code
- Code: Select all
Document doc = new Document();
doc.loadFromFile(inputPath);
ToPdfParameterList ppl = new ToPdfParameterList();
ppl.setPrivateFontPaths(getPrivateFontPaths());
ppl.isEmbeddedAllFonts(true);
ppl.setMemoryOptimization(true);
ppl.setDisableLink(false);
doc.setJPEGQuality(80);
doc.saveToFile(outputPath, ppl);
doc.close();
private List<PrivateFontPath> getPrivateFontPaths() {
//String fontLocation=Objects.requireNonNull(this.getClass().getClassLoader().getResource("fonts")).toString();
String fontLocation="..\\..\\..\\..\\..\\..\\..\\fonts\\";
List<PrivateFontPath> privateFontPaths = new ArrayList<>();
String fontName = "myfont";
privateFontPaths.add(new PrivateFontPath(fontName, PdfFontStyle.Bold.getValue(), fontLocation + "myfont.otf"));
return privateFontPaths;
}
I believe the issue is with location of the fonts when it is packages as a jar, as I want the otf file embedded in the jar so the fonts will work everywhere the jar is run.
I tried multiple approaches using a relative path, getting the location of the resource in code and using the absolute path where I removed the `file:` prefix, used with prefix.
So how can I add a custom font that is packaged inside the jar?