java - EJB服务PDF,导致重新请求对象属于其他PDF异常

标签 java pdf itext ejb indirect-objects

我正在使用 EJB 通过调用远程业务接口(interface)来通过 Internet 提供由 iTextPdf 7.0.4 创建的 pdf。

我的EJB服务器端实现为:

@Override
public byte[] renderBillById(String billId) {
    logger.info("rendering bill pdf called in EJB Model.");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = new PdfWriter(baos);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf, PageSize.A4);
    document.setMargins(20, 20, 20, 20);

    fetchBillContext(billId);

    document.add(billHeaderTable());
    document.add(billReceiverTable());
    document.add(billBalanceTable());
    document.add(summaryOuterTable());
    document.add(howToPay());
    document.add(notesPara());
    document.add(payOptionsTable());

    document.close();
    return baos.toByteArray();
}

服务器实现将生成一个 PDF,我将需要它的字节数组格式。我尝试通过Web服务器请求字节数组(通过使用 Controller 来服务JSF页面在Web服务器中调用它)并通过远程EJB客户端请求它(通过RMI调用它),我得到了相同的结果:我只能调用这个实现一次,当我尝试再次调用它时,EJB服务器抛出以下异常:

 com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document.

详细信息为:

javax.ejb.EJBException: EJB Exception: ; nested exception is: 
com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document.
at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.unwrapRemoteException(RemoteBusinessIntfProxy.java:127)
at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:94)
at com.sun.proxy.$Proxy306.renderBillById(Unknown Source)
at com.longz.ozssc.web.staff.BillControlBean.viewBillInPdf(BillControlBean.java:191)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.el.ELUtil.invokeMethod(ELUtil.java:304)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:535)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at org.primefaces.application.DialogActionListener.processAction(DialogActionListener.java:45)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:650)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at com.longz.ozssc.web.staff.misc.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:32)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3683)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3649)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2433)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2281)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2259)
at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1691)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1651)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:270)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:640)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:406)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
Caused By: com.itextpdf.kernel.PdfException: Pdf indirect object belongs to other PDF document. Copy object to current pdf document.
at weblogic.utils.StackTraceDisabled.unknownMethod()

服务器端异常看起来有一些对象引用问题。

我试图在堆栈溢出中找到类似的问题,其中大多数都认为这是 PdfFont 问题,但就我而言,我没有使用任何 PdfFont。

还有人遇到这个问题吗?如果你有。怎么解决呢?

最佳答案

请前往chapter 1 iText 7 Building Blocks 教程的一部分。搜索错误消息:

Pdf indirect object belongs to other PDF document. Copy object to current pdf document.

你会发现你是:

attempting to use a PdfFont instance that belongs to the PDF that was created the first time...

如果您阅读本章,您将了解两者之间的区别

  • FontProgram 对象,一个可以实例化一次并在每次创建 PDf 时重复使用的字体对象,以及
  • PdfFont 对象,这是您创建的每个 PDF 所独有的字体对象,并且应该为您创建的每个文档实例化该对象。

blagae所示,错误位于 billHeaderTable()billReceiverTable()、... 方法中的某个位置。

关于java - EJB服务PDF,导致重新请求对象属于其他PDF异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46665274/

相关文章:

java - 使用 iText 5 生成 pdf 文件时出现问题

java - 如何修改分配给JTree的对象?

java - 向 ArrayList 后面添加大量新项的时间复杂度

java - 将 String [] args 值传递给另一个类

java - 如何在PDFBox中为PDListBox设置多个选项?

java - 使用itext在java swing中查看pdf?

java - OpenCV/JavaCV 人脸识别 - 非常相似的置信度值

reactjs - 在react js中单击按钮生成pdf

c# - 文件已损坏且无法修复

java - lib 升级后 iText pdf 功能损坏 - 无法合并表格