This post is a follow-up to the post Using Alfresco Composite Rendition to Render PDF. In this tutorial we will update the Java code in order to persist the composite rendition that creates a PDF rendition using Apache FOP and XSL-FO as an intermediate format.
Follow Alfresco
When you persist a rendition in Alfresco, the system will update a rendition every time the document properties are updated. You can follow Alfresco’s rendition behaviour by adding the following line to the log4j.properties file:
log4j.logger.org.alfresco.repo.rendition=debug
Persist a rendition
To make a rendition persistent you need to store the rendition definition using the folowing method:
renditionService.saveRenditionDefinition(compositeDefinition);
In addition to adding this line, I also decided to check if the rendition definition already exists, since you call this code every time you add a new rendition to a document.
Updated code
In the following class the updates are highlighted:
package com.someco.action;
import java.util.List;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.rendition.executer.ReformatRenderingEngine;
import org.alfresco.repo.rendition.executer.XSLTRenderingEngine;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.rendition.CompositeRenditionDefinition;
import org.alfresco.service.cmr.rendition.RenditionDefinition;
import org.alfresco.service.cmr.rendition.RenditionService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
public class FoRenditionActionExecuter extends ActionExecuterAbstractBase {
public static final String NAME = "fo";
public static final String PARAM_TEMPLATE_REF = "template";
RenditionService renditionService;
public RenditionService getRenditionService() {
return renditionService;
}
public void setRenditionService(RenditionService renditionService) {
this.renditionService = renditionService;
}
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
QName compRenderingName = QName.createQName(
NamespaceService.CONTENT_MODEL_1_0_URI,
"compRenderingDefinition");
CompositeRenditionDefinition compositeDefinition = (CompositeRenditionDefinition) renditionService
.loadRenditionDefinition(compRenderingName);
if (compositeDefinition == null) {
RenditionDefinition xslDefinition = renditionService
.createRenditionDefinition(QName.createQName(
NamespaceService.CONTENT_MODEL_1_0_URI,
"xslRenderingDefinition"), XSLTRenderingEngine.NAME);
NodeRef template = (NodeRef) action
.getParameterValue(PARAM_TEMPLATE_REF);
xslDefinition.setParameterValue(
XSLTRenderingEngine.PARAM_TEMPLATE_NODE, template);
xslDefinition.setParameterValue(
ReformatRenderingEngine.PARAM_MIME_TYPE, "text/xsl");
RenditionDefinition pdfDefinition = renditionService
.createRenditionDefinition(QName.createQName(
NamespaceService.CONTENT_MODEL_1_0_URI,
"pdfRenderingDefinition"),
ReformatRenderingEngine.NAME);
pdfDefinition.setParameterValue(
ReformatRenderingEngine.PARAM_MIME_TYPE,
MimetypeMap.MIMETYPE_PDF);
compositeDefinition = renditionService
.createCompositeRenditionDefinition(compRenderingName);
compositeDefinition.addAction(xslDefinition);
compositeDefinition.addAction(pdfDefinition);
renditionService.saveRenditionDefinition(compositeDefinition);
}
renditionService.render(actionedUponNodeRef, compositeDefinition);
}
@Override
protected void addParameterDefinitions(List paramList) {
paramList.add(new ParameterDefinitionImpl(PARAM_TEMPLATE_REF,
DataTypeDefinition.NODE_REF, true,
getParamDisplayLabel(PARAM_TEMPLATE_REF)));
}
}
Test the rendition
Now if you create a rendition using this custom action, the system will first check if there is already a rendition definition with the same name. If this is not the case, the system persists the rendition definition and creates the rendition.
On any properties update, the system will update the rendition. If you added the log statement to the log properties file, you can trace the rendition engine’s behaviour.
You can check the rendition by locating the document in the node browser. By default the rendition is stored as a hidden child node of the source document.
-
bpeters posted this