cancel
Showing results for 
Search instead for 
Did you mean: 

Word to PDF: NullPointerException in JODConverter

nicolasraoul
Star Contributor
Star Contributor
In Java, I transform a Word file to a PDF file, but get a NullPointerException in jodconverter. It seems that officeManager is null. Here is the exception:

org.alfresco.service.cmr.repository.ContentIOException: 02100003 Content conversion failed: 
   reader: ContentAccessor[ contentUrl=store://2010/3/10/13/59/458146e2-60db-4cf1-91fb-b93ab70aa4d3.bin, mimetype=application/msword, size=9216, encoding=UTF-8, locale=ja_JP]
   writer: ContentAccessor[ contentUrl=store://2010/3/10/14/0/a2d01abf-d7d5-48cf-b3d3-d3e4948cac30.bin, mimetype=application/pdf, size=0, encoding=UTF-8, locale=ja_JP]
   options: org.alfresco.service.cmr.repository.TransformationOptions@17af7af
   at org.alfresco.repo.content.transform.AbstractContentTransformer2.transform(AbstractContentTransformer2.java:176)
   at org.alfresco.repo.content.transform.AbstractContentTransformer2.transform(AbstractContentTransformer2.java:143)
   at com.company.PublishActionHandler$4.doWork(PublishActionHandler.java:185)

   at org.jbpm.graph.def.Action.execute(Action.java:129)
   at org.jbpm.graph.def.GraphElement.executeAction(GraphElement.java:284)
   … 95 more
Caused by: java.lang.NullPointerException
   at org.artofsolving.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:75)
   at org.artofsolving.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:66)
   at org.alfresco.enterprise.repo.content.transform.JodContentTransformer.transform(JodContentTransformer.java:228)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.alfresco.repo.management.subsystems.SubsystemProxyFactory$1.invoke(SubsystemProxyFactory.java:77)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy34.transform(Unknown Source)
   at org.alfresco.repo.content.transform.ProxyContentTransformer.transformInternal(ProxyContentTransformer.java:66)
   at org.alfresco.repo.content.transform.AbstractContentTransformer2.transform(AbstractContentTransformer2.java:167)
   … 112 more

And here is my code:

       ContentReader reader = contentService.getReader(original, ContentModel.PROP_CONTENT);
       ContentWriter pdfWriter = contentService.getWriter(pdf, ContentModel.PROP_CONTENT, true);
       ContentTransformer pdfTransformer =
              contentService.getTransformer(originalMimeType, MimetypeMap.MIMETYPE_PDF);
      
       // Transform as admin, because the place where we write might require high privileges.
       final ContentTransformer finalPdfTransformer = pdfTransformer;
       final ContentReader finalReader = reader;
       final ContentWriter finalPdfWriter = pdfWriter;
      RunAsWork<Boolean> work2 = new RunAsWork<Boolean>() {
           public Boolean doWork() throws Exception {
              finalPdfTransformer.transform(finalReader, finalPdfWriter);
              return true;
            }
        };
        AuthenticationUtil.runAs(work2, "admin");

Word to PDF transformation works fine as a content rule, but does not work with this Java code.

Any idea?
Maybe a sort of connection with OpenOffice is sometimes lost?
Thanks a lot!
7 REPLIES 7

rascio
Champ in-the-making
Champ in-the-making
i've the same problem…have you solved it?

nicolasraoul
Star Contributor
Star Contributor
i've the same problem…have you solved it?
Not yet. I am busy on other aspects of the project, but this problem is a huge threat on the whole project 😞

nicolasraoul
Star Contributor
Star Contributor
I had disabled the PDF feature so I hadn't looked at this for ages.
Today I gave it another chance, and surprise, it works! 🙂
I have no idea what was the problem 😞

rascio
Champ in-the-making
Champ in-the-making
yes for me is the same…
now it works correctly…bah it's random

kimmoilppola
Champ in-the-making
Champ in-the-making
There, I fixed it!
It's not random, I looked through Alfresco sources and the actual transformation needs to be done in it's own transaction. I don't know why it needs it and the exception we get is not very hintful -
but here's a working example:


        ContentService contentService = serviceRegistry.getContentService();
        NodeService nodeService = serviceRegistry.getNodeService();
        MimetypeService mimetypeService = serviceRegistry.getMimetypeService();

        String newName = transformName(mimetypeService, document.getName(), targetMimeType, true);
        NodeRef parentNodeRef = (nodeService.getPrimaryParent(sourceNodeRef)).getParentRef();

        NodeRef targetNodeRef = serviceRegistry.getCopyService().copy(
                sourceNodeRef,
                parentNodeRef,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "copy"),
                false);

        nodeService.setProperty(targetNodeRef, ContentModel.PROP_NAME, newName);

        ContentReader reader = contentService.getReader(sourceNodeRef, ContentModel.PROP_CONTENT);
        String sourceMimeType = reader.getMimetype();

        if (null == reader || !reader.exists()) {
            throw new ContentException("Could not aqcuire reader");
        }

        ContentWriter writer = contentService.getWriter(targetNodeRef, ContentModel.PROP_CONTENT, true);
        writer.setMimetype(targetMimeType);
        writer.setEncoding(reader.getEncoding());

        if (null != contentService.getTransformer(sourceMimeType, targetMimeType)) {
            serviceRegistry.getTransactionService().getRetryingTransactionHelper().doInTransaction(doTransformationCallback(sourceNodeRef, targetNodeRef, reader, writer), false, true);         
        }
    }

    protected RetryingTransactionHelper.RetryingTransactionCallback doTransformationCallback(final NodeRef sourceNodeRef, final NodeRef targetNodeRef, final ContentReader reader, final ContentWriter writer) {
        return new RetryingTransactionHelper.RetryingTransactionCallback() {
            @Override
            public Object execute() throws Throwable {
                TransformationOptions options = new TransformationOptions(
                        sourceNodeRef, ContentModel.PROP_NAME, targetNodeRef, ContentModel.PROP_NAME);
                serviceRegistry.getContentService().transform(reader,writer,options);
                return null;
            }
        };
    }

kimmoilppola
Champ in-the-making
Champ in-the-making
Sorry I cheered too early, it still fails randomly but seems to work more often.

neilm
Champ in-the-making
Champ in-the-making
If anyone does manage to get a test case that reproduces this failure, please JIRA it.
Thank you!