View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.thumbnail.impl;
18  
19  import java.awt.image.BufferedImage;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.List;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  
27  import javax.jcr.Node;
28  
29  import org.exoplatform.container.component.ComponentPlugin;
30  import org.exoplatform.container.xml.InitParams;
31  import org.exoplatform.services.cms.thumbnail.ThumbnailPlugin;
32  import org.exoplatform.services.log.ExoLogger;
33  import org.exoplatform.services.log.Log;
34  import org.icepdf.core.exceptions.PDFException;
35  import org.icepdf.core.exceptions.PDFSecurityException;
36  import org.icepdf.core.pobjects.Document;
37  import org.icepdf.core.pobjects.Page;
38  import org.icepdf.core.pobjects.Stream;
39  import org.icepdf.core.util.GraphicsRenderingHints;
40  
41  /**
42   * Created by The eXo Platform SARL
43   * Author : Dang Van Minh
44   *          minh.dang@exoplatform.com
45   * OCt 22, 2009
46   * 2:20:33 PM
47   */
48  public class PDFThumbnailPlugin implements ComponentPlugin, ThumbnailPlugin {
49  
50    private ThumbnailType config;
51    private String description;
52    private String name;
53    private static final Log LOG = ExoLogger.getExoLogger(PDFThumbnailPlugin.class.getName());
54  
55    public PDFThumbnailPlugin(InitParams initParams) throws Exception {
56      config = initParams.getObjectParamValues(ThumbnailType.class).get(0);
57    }
58  
59    public String getDescription() {
60      return description;
61    }
62  
63    public String getName() {
64      return name;
65    }
66  
67    public void setDescription(String description) {
68      this.description = description;
69    }
70  
71    public void setName(String name) {
72      this.name = name;
73    }
74  
75    public BufferedImage getBufferedImage(Node contentNode, String nodePath) throws Exception {
76      Document document = new Document();
77  
78      // Turn off Log of org.icepdf.core.pobjects.Stream to not print error stack trace in case
79      // viewing a PDF file including CCITT (Fax format) images
80      // TODO: Remove these statement and comments after IcePDF fix ECMS-3765
81      Logger.getLogger(Stream.class.toString()).setLevel(Level.OFF);
82  
83      try {
84        InputStream input = contentNode.getProperty("jcr:data").getStream() ;
85        document.setInputStream(input, nodePath);      
86      } catch (PDFException ex) {
87        if (LOG.isWarnEnabled()) {
88          LOG.warn("Error parsing PDF document " + ex);
89        }
90      } catch (PDFSecurityException ex) {
91        if (LOG.isWarnEnabled()) {
92          LOG.warn("Error encryption not supported " + ex);
93        }
94      } catch (FileNotFoundException ex) {
95        if (LOG.isWarnEnabled()) {
96          LOG.warn("Error file not found " + ex);
97        }
98      } catch (IOException ex) {
99        if (LOG.isWarnEnabled()) {
100         LOG.warn("Error handling PDF document " + contentNode.getPath());
101       }
102     }
103     // Paint each pages content to an image and write the image to file
104     BufferedImage image = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN,
105         Page.BOUNDARY_CROPBOX, 0.0f, 1.0f);
106     document.dispose();
107     return image;
108   }
109 
110   public List<String> getMimeTypes() {
111     return config.getMimeTypes();
112   }
113 
114 }