cancel
Showing results for 
Search instead for 
Did you mean: 

transformDocument() in Java, not in Javascript?

talija83
Champ in-the-making
Champ in-the-making
Is there a way to easily transform content from one mimetype to another, using Java?  :?:  (For example, HTML file to Word document.)

I know that it can be done easily using transformDocument() function in Javascript, but I'm interested in how it's done in Java. In Alfresco 3.3g, it seems that custom actions done in JS, triggered from web-client, have some problem with permissions, therefore this script needs to be translated to Java. I've tried using ContentWriter, ContentReader and ContentTransformer, but the document I get in Word cannot be opened if it contains a photo - it says it's too big to be opened.  :!:

Basically, what I'm trying to do is 'translate' this:

var word_doc = html_doc.transformDocument("application/msword");

into something like this:

 private void transformDocument(NodeRef sourceNodeRef, NodeRef targetNodeRef, String sourceMimetype, String targetMimetype)
    {
      ContentService contentService = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService();      
      ContentReader reader = contentService.getReader(sourceNodeRef, ContentModel.PROP_CONTENT);
      
      // If there is a reader…
      if (reader.exists() && reader != null)
      {
         // …we create the transformer…
         ContentTransformer transformer = contentService.getTransformer(sourceMimetype, targetMimetype);
         
                        // …and if it is valid…
         if (transformer != null)
         {
            ContentWriter writer = contentService.getWriter(targetNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setMimetype(targetMimetype);
            
            try
            {
                                        // …we transform the content.
               transformer.transform(reader, writer);

               reader = writer.getReader();
               
               if (!reader.exists())
                  throw new ContentIOException("The transformation did not write any content, yet: \n" + " transformer: " + transformer + "\n" + " temp writer: " + writer);
            }
            
            catch (ContentIOException e) {
               logger.debug(e.getMessage());
            }
         }
         else
            logger.debug("Transformer wasn't created successfully…");
      }
      else
         logger.debug("Reader doesn't exist…");
    }

The call in Java would now be:

transformDocument(html_doc, word_doc, MimetypeMap.MIMETYPE_HTML, MimetypeMap.MIMETYPE_WORD);

Does anyone have an idea how could this be done more easily? And without any strange messages from MS Office, when reading the converted document later?

Thanks in advance.  Smiley Happy
3 REPLIES 3

hsohaib
Champ on-the-rise
Champ on-the-rise
the javascript used is a server side java code, so we can translate :


var html_doc = … // Reference your doc
var word_doc = html_doc.transformDocument("application/msword");

to :


ScriptNode html_doc = new ScriptNode(htmlDocNodeRef, serviceRegistry);
NodeRef word_doc = html_doc.transformDocument("application/msword").getNodeRef();

hope it works for you.

mrogers
Star Contributor
Star Contributor
The JAVA api is ContentService.transform()

talija83
Champ in-the-making
Champ in-the-making
Thanks for both of your replies.  Smiley Happy

Eventually, it seems like there was a problem with using a proper version of OpenOffice, so the conversion wasn't done the way it was supposed to. I've tried both of your solutions, and they work, too! I think they'll serve me for future use. :idea: