由于我在使用iText时使用了外部字体,因此我需要从我的Maven资源文件夹(src/main/resources)加载字体文件,但是iText API要求我提供文件路径而不是文件本身。
因此,当我尝试使用以下代码时出现问题:
String arialUnicodeFontPath = getClass().getClassLoader().getResource("arialuni.ttf").getPath();
BaseFont unicodeBaseFont =
BaseFont.createFont(arialUnicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
I get this:
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