Friday, 20 February 2015

Get document PDF preview in Alfresco

Alfresco is able to transform document types.
For example transform MSWord doc file into PDF

The quick way to access PDF preview (or of any other available type) is a ThumbnailService

An example:
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
//...
public class YourClass{
  //...
  @Autowired
  ThumbnailService thumbnailService;
  //...
  public yourMethod {
      NodeRef documentNodeRef = new NodeRef(
          request.getParameter("nR")
      );
      NodeRef pdfPreviewNodeRef = null;
      //...
      pdfPreviewNodeRef = thumbnailService.getThumbnailByName(
          documentNodeRef, ContentModel.PROP_CONTENT, "pdf"
      );
      if (pdfPreviewNodeRef==null) {
        //generate a new PDF preview if one is not available yet
        pdfPreviewNodeRef = thumbnailService.createThumbnail(
            documentNodeRef, ContentModel.PROP_CONTENT,
            "application/pdf", new SWFTransformationOptions(), "pdf"
        );
      }
      //...
  }
  //...
}