alfrescoalfresco-maven

How can i append a pdf into another? Alfresco


How can i append a pdf into another?

I tried using this code, but I am getting java.lang.NullPointerException when i try to getContentInputStream. what am I doing wrong? How can I attach one pdf to another?

PDDocument pdfTarget = null;
InputStream is = null;
InputStream tis = null;
for (ChildAssociationRef file: quotationsFiles) {
 try {
  NodeRef toAppend = file.getChildRef(); //workspace://SpacesStore/11bce382-45bf-4c67-95bc-a65361b323ef

  ContentReader append = getReader(toAppend);
  is = append.getContentInputStream(); // Here iam getting java.lang.NullPointerException 

  NodeRef targetNodeRef = reportFile.getNodeRef();
  ContentReader targetReader = getReader(targetNodeRef);
  tis = targetReader.getContentInputStream();

  String fileName = String.valueOf(serviceRegistry.getNodeService().getProperty(targetNodeRef, ContentModel.PROP_NAME));

  // stream the document in
  pdf = PDDocument.load(is);
  pdfTarget = PDDocument.load(tis);

  // Append the PDFs
  PDFMergerUtility merger = new PDFMergerUtility();
  merger.appendDocument(pdfTarget, pdf);
  merger.setDestinationFileName(fileName);
  merger.mergeDocuments();
 } catch (Exception e) {
  //throw new AlfrescoRuntimeException("IOException", e);
  ColorLogUtil.debug(LOGGER, "IOException Error caused by :" + e);
 }
}
private ContentReader getReader(NodeRef nodeRef) {
 if (serviceRegistry.getNodeService().exists(nodeRef) == false) {
  throw new AlfrescoRuntimeException("NodeRef: " + nodeRef + " does not exist");
 }

 QName typeQName = serviceRegistry.getNodeService().getType(nodeRef);
 if (serviceRegistry.getDictionaryService().isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) {
  throw new AlfrescoRuntimeException("The selected node is not a content node");
 }

 ContentReader contentReader = serviceRegistry.getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT);

 if (contentReader == null) {
  throw new AlfrescoRuntimeException("The content reader for NodeRef: " + nodeRef + "is null");
 }

 return contentReader;
}


Solution

  • See if this code works for you:

     public NodeRef mergePdfs(List<NodeRef> nodeRefList, String fileName,NodeRef destinationNode) 
                throws FileNotFoundException,FileExistsException,Exception {
            InputStream originalInputStream = null;
            ContentReader reader = null;
            NodeRef newDocNoderef = null;
            PDFMergerUtility PDFmerger = new PDFMergerUtility();
            ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
    
        try {
            LOGGER.debug("Merging of Doc Started");
            for (NodeRef node : nodeRefList) {
                reader = contentService.getReader(node, ContentModel.PROP_CONTENT);
                originalInputStream = reader.getContentInputStream();
                PDFmerger.addSource(originalInputStream);
            }
            PDFmerger.setDestinationStream(outputstream);
            PDFmerger.mergeDocuments();
            if(originalInputStream!=null) {
                originalInputStream.close();
            }
            newDocNoderef = writeContentToAlfresco(outputstream, nodeRefList, fileName,destinationNode);
            LOGGER.debug("Documents are merged and new pdf is created at "+newDocNoderef);
    
        } finally {
            if(outputstream!=null)
                outputstream.close();
        }
        return newDocNoderef;
    }
    
    public NodeRef writeContentToAlfresco(ByteArrayOutputStream outputstream, List<NodeRef> childRefList,
            String fileName,NodeRef destinationNode) throws FileExistsException,IOException,Exception {
        NodeRef pdf = null;
        Map<QName, Serializable> props =  new HashMap<>();
        Map<Date, NodeRef> dateMap = new HashMap<Date, NodeRef>();
        NodeRef parentNodeRef=null;
        try {
            LOGGER.debug("Upload to Alfresco Started");
    
            for(NodeRef noderef : childRefList) {
                Date date = (Date) nodeService.getProperty(noderef, ContentModel.PROP_MODIFIED);
                dateMap.put(date, noderef);
            }
            Map<Date, NodeRef> m1 = new TreeMap<Date, NodeRef>(dateMap);
            Map.Entry<Date, NodeRef> entry = m1.entrySet().iterator().next();
            NodeRef finalnodeRef = entry.getValue();
    
            if(destinationNode!=null) {
                parentNodeRef = destinationNode;
            }else {
                parentNodeRef = nodeService.getPrimaryParent(finalnodeRef).getParentRef();
            }
    
    
            QName[] myModelProps = CommonConstants.myModelProps;
            for (QName myModelProp : myModelProps) {
                Serializable object = nodeService.getProperty(finalnodeRef, myModelProp);
                props.put(myModelProp, object);
            }
    
            FileInfo pdfInfo = fileFolderService.create(parentNodeRef, fileName + ".pdf",
                    MyModel.TYPE_CUSTOM_MYMODEL_TYPE);
            pdf = pdfInfo.getNodeRef();
    
            nodeService.setProperties(pdf,props);
            nodeService.setProperty(pdf, ContentModel.PROP_TITLE,
                    nodeService.getProperty(finalnodeRef, ContentModel.PROP_TITLE));
            nodeService.setProperty(pdf, ContentModel.PROP_DESCRIPTION, 
                    nodeService.getProperty(finalnodeRef, ContentModel.PROP_DESCRIPTION));
            nodeService.setProperty(pdf,ContentModel.PROP_NAME,fileName + ".pdf");
    
            ContentWriter writer = contentService.getWriter(pdf, ContentModel.PROP_CONTENT, true);
            writer.setMimetype(MimetypeMap.MIMETYPE_PDF);
            writer.setEncoding("UTF-8");
            writer.putContent(new ByteArrayInputStream(outputstream.toByteArray()));
    
            LOGGER.debug("Upload to Alfresco Ended");
        }  catch(FileExistsException fee) {
            ExceptionUtils.printRootCauseStackTrace(fee);
            throw new FileExistsException(parentNodeRef, fileName);
        }
        catch (Exception e) {
            ExceptionUtils.printRootCauseStackTrace(e);
            throw new Exception(e);
        } finally {
            if (outputstream != null)
                outputstream.close();
        }
    
        return pdf;
    }