1

I am using EJB to serve a pdf that created by iTextPdf 7.0.4 over Internet by call remote business interface.

My EJB server side implement as:

@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();
}

The server implement will generate a PDF and I will need it in byte array format. I tried request the byte array over Web server (call it in web server by using controller to server a JSF page) and request it over remote EJB client (call it over RMI), I got same result as: I can only call this implement once, when I tried call it again, EJB server throws following exception:

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

Details as:

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()

The server side exception looks like have some Object reference issue.

I tried to found out the similar issue in stack overflow, most of them regarding it is a PdfFont issue, but in my case, I didn't used any PdfFont.

Does anyone else have the problem? if you have. How do you fix it?

cidy.long
  • 417
  • 12
  • 35
  • 1
    This can pretty much only be a problem in one of the methods you reference in the `document.add` calls, so please share the code for your `billHeaderTable()` and similar methods. – blagae Oct 10 '17 at 12:42
  • PdfFont object never be used in my PDF document. That is why I am very confused. – cidy.long Oct 11 '17 at 03:24

1 Answers1

0

Please go to chapter 1 of the iText 7 Building Blocks tutorial. Search for the error message:

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

You will discover that you are:

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

If you read the chapter, you will learn about the difference between

  • The FontProgram object, a font object that can be instantiated once and reused every time you create a PDf, and
  • The PdfFont object, a font object that is unique to each PDF that you create, and that should be instantiated for every document you create.

As indicated by blagae, the error is somewhere in the billHeaderTable(), billReceiverTable(),... methods.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks! Mate. PdfFont object never be used in my PDF document. That is why I am very confused. – cidy.long Oct 10 '17 at 21:09
  • In that case, you are reusing another object you shouldn't reuse. We can't tell you which one, because you don't show us the relevant code. – Bruno Lowagie Oct 11 '17 at 04:25