PdfBox 2.0.3 NullPointerException在Linux上的字体编码

时间:2016-10-10 23:31:25

标签: java linux fonts pdfbox

我目前正在使用Apache PdfBox 2.0.3在Linux上遇到问题。

您可以从代码段和下面的例外中看到。我尝试做的只是设置字体,然后在页面上写。不幸的是,在Ubuntu上使用Tomcat 7和Java 7进行我们的linux测试和实时环境是不成功的。在我的Windows开发机器上,一切顺利。

我已经尝试使用内置的helvetica和currier字体,你在片段中看到的ttf是从google fonts下载的。

你知道我做错了什么吗?

我使用的代码片段:

PDPage page = detailsDocument.getPage(0);
        PDPageContentStream contentStream = new PDPageContentStream(detailsDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);
        File fontFile = new File(getClass().getClassLoader().getResource("fonts/OpenSans-Regular.ttf").getFile());
        PDFont font = PDTrueTypeFont.load(document, fontFile, Encoding.getInstance(COSName.STANDARD_ENCODING));

        contentStream.beginText();
        contentStream.setFont( font, 22 );
        contentStream.newLineAtOffset(57, 495);
        contentStream.showText( collection.getDocument().getIndication().getName() + " - " + collection.getDocument().getTitle() );
        contentStream.endText();

例外:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)

root cause

java.lang.NullPointerException
    org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:311)
    org.apache.pdfbox.pdmodel.PDPageContentStream.showText(PDPageContentStream.java:414)
    hu.jacsomedia.tms.webapp.pdfbuilder.generators.PdfGenerator.addOrderDetailsPage(PdfGenerator.java:361)
    hu.jacsomedia.tms.webapp.pdfbuilder.generators.PdfGenerator.generatePdf(PdfGenerator.java:68)
    hu.jacsomedia.tms.webapp.pdfbuilder.generators.PdfGenerator.generatePdfToOMR(PdfGenerator.java:268)
    hu.jacsomedia.tms.webapp.pdfbuilder.generators.PdfGenerator$$FastClassBySpringCGLIB$$fcaef06.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    hu.jacsomedia.tms.webapp.pdfbuilder.generators.PdfGenerator$$EnhancerBySpringCGLIB$$55740b22.generatePdfToOMR(<generated>)
    hu.jacsomedia.tms.webapp.pdfbuilder.controller.PdfGeneratorController.generate(PdfGeneratorController.java:139)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274)
    org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject.java:536)
    org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
    org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)

1 个答案:

答案 0 :(得分:1)

实际上两位评论者都是对的。 我做了以下两件事来解决这个问题:

  • 在我的类路径中有2.0.0和2.0.3版本。我不得不用maven重新下载所有内容。
  • 根据mlk的评论我改变了编码ot COSName.WIN_ANSI_ENCODING

此外,我对我尝试写入文件的每个变量进行了仔细检查,以防止NULL参数和showText调用。