1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
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
79
80
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
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 }