2
由於我爲iText使用外部字體,我需要從我的maven資源文件夾(src/main/resources)加載字體文件,但是iText API需要我提供一個文件路徑,而不是文件本身。從maven資源文件夾獲取正確的文件路徑 - iText
所以,我有問題,而試圖使用此代碼:
String arialUnicodeFontPath = getClass().getClassLoader().getResource("arialuni.ttf").getPath();
BaseFont unicodeBaseFont =
BaseFont.createFont(arialUnicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
我得到這個:
2016-08-18 09:17:12,667 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/pdfgen2].[controlServlet]] (http-/0.0.0.0:8443-1) JBWEB000236: Servlet.service() for servlet controlServlet threw exception: java.io.IOException: /content/pdfgen2.war/WEB-INF/classes/arialuni.ttf not found as file or resource.
at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:248) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:148) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:641) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.TrueTypeFontUnicode.process(TrueTypeFontUnicode.java:121) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.TrueTypeFontUnicode.<init>(TrueTypeFontUnicode.java:98) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:704) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:622) [itextpdf-5.5.3.jar:5.5.3]
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:457) [itextpdf-5.5.3.jar:5.5.3]
是'pdfgen2.war'包含文件你的申請?是位於那裏的文件'arialuni.ttf'? – Jens
是的,它是我的應用程序,文件夾結構如上所述並打開戰爭文件本身的結構是:WEB-INF/classes/arialuni.ttf –
嘗試在文件名前加一個「/」 – Jens