Thumbnail Service: Added PDF to Image transformer and added complex transformers to allow documents to be thumbnailed as images

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9997 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2008-07-23 20:56:02 +00:00
parent 08a552304e
commit ff822d1211
3 changed files with 192 additions and 2 deletions

View File

@@ -0,0 +1,125 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.content.transform;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.alfresco.util.TempFileProvider;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
/**
* Makes use of the {@link http://www.pdfbox.org/ PDFBox} library to
* perform conversions from PDF files to images.
*
* @author Roy Wetherall
*/
public class PdfToImageContentTransformer extends AbstractContentTransformer2
{
/**
* Currently the only transformation performed is that of text extraction from PDF documents.
*/
public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
{
if (MimetypeMap.MIMETYPE_PDF.equals(sourceMimetype) == true &&
MimetypeMap.MIMETYPE_IMAGE_PNG.equals(targetMimetype) == true)
{
// only support PDF -> PNG
return true;
}
else
{
return false;
}
}
protected void transformInternal(
ContentReader reader,
ContentWriter writer,
TransformationOptions options) throws Exception
{
RandomAccessFile raf;
try
{
File file = TempFileProvider.createTempFile("pdfToImage", ".pdf");
reader.getContent(file);
raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
PDFPage page = pdffile.getPage(0);
//get the width and height for the doc at the default zoom
int width=(int)page.getBBox().getWidth();
int height=(int)page.getBBox().getHeight();
Rectangle rect = new Rectangle(0,0,width,height);
int rotation=page.getRotation();
Rectangle rect1=rect;
if (rotation==90 || rotation==270)
rect1=new Rectangle(0,0,rect.height,rect.width);
//generate the image
BufferedImage img = (BufferedImage)page.getImage(
rect.width, rect.height, //width & height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
File outputFile = TempFileProvider.createTempFile("pdfToImageOutput", ".png");
ImageIO.write(img, "png", outputFile);
writer.putContent(outputFile);
}
catch (FileNotFoundException e1)
{
throw new AlfrescoRuntimeException("Unable to create image from pdf file.", e1);
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Unable to create image from pdf file.", e);
}
}
}

View File

@@ -30,6 +30,7 @@ import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.repo.node.NodeRefPropertyMethodInterceptor;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
@@ -37,6 +38,8 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Create thumbnail action executer.
@@ -47,6 +50,8 @@ import org.alfresco.service.namespace.QName;
*/
public class CreateThumbnailActionExecuter extends ActionExecuterAbstractBase
{
private static Log logger = LogFactory.getLog(CreateThumbnailActionExecuter.class);
/** Thumbnail Service */
private ThumbnailService thumbnailService;
@@ -105,8 +110,15 @@ public class CreateThumbnailActionExecuter extends ActionExecuterAbstractBase
contentProperty = ContentModel.PROP_CONTENT;
}
// Create the thumbnail
this.thumbnailService.createThumbnail(actionedUponNodeRef, contentProperty, details.getMimetype(), details.getTransformationOptions(), thumbnailName, null);
try
{
// Create the thumbnail
this.thumbnailService.createThumbnail(actionedUponNodeRef, contentProperty, details.getMimetype(), details.getTransformationOptions(), thumbnailName, null);
}
catch (Exception exception)
{
logger.info("Creation of thumbnail '" + details.getName() + "' failed");
}
}
}