cancel
Showing results for 
Search instead for 
Did you mean: 

Update Document Template Dynamically in Alfresco from property page

deeps
Champ in-the-making
Champ in-the-making
Hii All,  


I have one document template as like attached screen shot. now my requirement is i want to dynamically update some values  of that document when i will upload it in alfresco site. i will either create that Document through node template or upload. I want to provide those values from document property page.
can anyone help me how can i achieve this.

I tried it using java script, but i am getting some problem. so i am thinking for using Alfresco CMIS API. But i am quite new in alfresco cmis. so can any one tell the better way which i can use for this task.

Thanks in advance
3 REPLIES 3

steven_okennedy
Star Contributor
Star Contributor
Hi deeps,

First question here is whether the population of the template only happens on initial upload or whether it should update the doc every time the properties get updated (I'm assuming when you say document properties page, you're also uploading through Share?). 

In the first case, it's worth bearing in mind that Share doesn't give you a location to enter values for properties before the document is uploaded, so it's a bit difficult to see where the values are going to come from in this case without a custom upload dialog (or use an exisitng extension that does this from addons.alfresco.com).  The second more general case is actually reasonably easy to do with a custom behaviour that fires when you update certain properties. 

The second question is around the format of the documents that you'll be dealing with.  If they are simple text-based formats, like txt, html etc then nice and straight-forward, but other document formats will require specific libraries to write the values into the documents - also if you need to worry about multiple document types (e.g. Word docs & Spreadsheets), you'll to do write your logic for each of the libraries required.

For the behaviour, Jeff Potts' acrticle as part of the Dev Series is very useful (http://ecmarchitect.com/alfresco-developer-series-tutorials/behaviors/tutorial/tutorial.html).  This gives you mechanism to capture the "event" of setting/changing the properties and then go and run your Java code that does the work.  Equally, you could use a custom action kicked off by a rule (http://ecmarchitect.com/alfresco-developer-series-tutorials/actions/tutorial/tutorial.html) if you need more flexibility around where/when you want the work to happen.

For the real work itself, you can use a simple find & replace mechanism based on the content text if your documents are text based, or you can use open source libraries like docx4j or Apache POI or there are some good proprietary products like Aspose well.  I'd recommend delimiting the placeholders in your template very clearly, e.g. surrounding them with "% %" or prefixing with $ or something similar, so you can pattern match effectively.  Also find and replace is easy in principal but you can end up with very messy looking documents if you don't take the necessary pains to limit and clean your inputs.

Regards

Steven

Hi Steven, Actually my requirement is changed a bit. now i want to extract some data from excel sheet and i want to inject in to word document template.

Now my Actual requirement is, i have suppose 10 employee information in excel sheet. i already read this information using poi api. Now i have one appraisal letter template, and based on excel sheet data i want to create 10 appraisal letters. means appraisal letter template is fixed for every employee. so i just want to replace some values like Employee id, Employee name, Employee Grade etc. Now i have two challenges. 1st is to replace info in doc template. and 2nd is where to keep where to keep doc template. Because i am planning to create one custom action to upload excel file, and on that action i will call java code in the back end. Can you suggest the way… whether should i keep that template in the document library or somewhere else?? Thanks in advance. 

Hi deeps,

If the code is going to be across more than one site, the best place to put this is probably in the Data Dictionary.  Alfresco already provides a location (Repository->Data Dictionary->Node Templates) for storing templates, and this location also gives the added benefit that Share also uses it to drive the "Create document from template" functionality from the Create button in the document library - so if you want to manually create documents using this template this gives you this option as well. 

If you don't want users to be able to manually create documents based on the template, you can easily create a new folder of your own (e.g. System Templates or whatever you like) within the Data Dictionary instead.  All you'll need in your logic is the NodeRef of the template file to pass to the CopyService, so essentially you can put the templates anywhere you like, but the Data Dictionary is a good place for content like this that's system wide. 

If the templates are site-specific (e.g. different sites need different templates), then it may make sense to use a convention and create a static named folder within each site's document library where the templates will live, but this is more manual work every time a site is set up and is prone to breaking (e.g. accidental deletion).

If you've gotten the hang of the POI libraries, then your 1st challenge shouldn't be too bad, although writing the updated content might be a little tricky as Alfresco won't allow the POI library to modify the content directly since it treats content as completely immutable.  Probably what you need to do is something like:
<ol>
<li>Use the CopyService to create the new node from the template</li>
<li>Get the new node's content using the ContentService.getContentReader() – you may want to consider writing to a temp file if you don't want to hold the entire file in memory</li>
<li>do your POI replacements and use ContentService.getContentWriter() on your new node to update the content – call writer.putContent() to stream in the content either from memory or from your temp file</li>
<li>Repeat!</li>
</ol>

Regards

Steven