001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.image.ImageToolImpl;
018    import com.liferay.portal.kernel.image.ImageTool;
019    
020    import java.awt.image.BufferedImage;
021    import java.awt.image.RenderedImage;
022    
023    import java.io.File;
024    
025    import java.util.List;
026    
027    import javax.imageio.ImageIO;
028    
029    import org.apache.pdfbox.pdmodel.PDDocument;
030    import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
031    import org.apache.pdfbox.pdmodel.PDPage;
032    
033    /**
034     * @author Juan Gonzalez
035     */
036    public class LiferayPDFBoxConverter {
037    
038            public LiferayPDFBoxConverter(
039                    File inputFile, File thumbnailFile, File[] previewFiles,
040                    String extension, String thumbnailExtension, int dpi, int height,
041                    int width, boolean generatePreview, boolean generateThumbnail) {
042    
043                    _inputFile = inputFile;
044                    _thumbnailFile = thumbnailFile;
045                    _previewFiles = previewFiles;
046                    _extension = extension;
047                    _thumbnailExtension = thumbnailExtension;
048                    _dpi = dpi;
049                    _height = height;
050                    _width = width;
051                    _generatePreview = generatePreview;
052                    _generateThumbnail = generateThumbnail;
053            }
054    
055            public void generateImagesPB() throws Exception {
056                    try (PDDocument pdDocument = PDDocument.load(_inputFile)) {
057                            PDDocumentCatalog pdDocumentCatalog =
058                                    pdDocument.getDocumentCatalog();
059    
060                            List<PDPage> pdPages = pdDocumentCatalog.getAllPages();
061    
062                            for (int i = 0; i < pdPages.size(); i++) {
063                                    PDPage pdPage = pdPages.get(i);
064    
065                                    if (_generateThumbnail && (i == 0)) {
066                                            _generateImagesPB(
067                                                    pdPage, _thumbnailFile, _thumbnailExtension);
068                                    }
069    
070                                    if (!_generatePreview) {
071                                            break;
072                                    }
073    
074                                    _generateImagesPB(pdPage, _previewFiles[i], _extension);
075                            }
076                    }
077            }
078    
079            private void _generateImagesPB(
080                            PDPage pdPage, File outputFile, String extension)
081                    throws Exception {
082    
083                    RenderedImage renderedImage = pdPage.convertToImage(
084                            BufferedImage.TYPE_INT_RGB, _dpi);
085    
086                    ImageTool imageTool = ImageToolImpl.getInstance();
087    
088                    if (_height != 0) {
089                            renderedImage = imageTool.scale(renderedImage, _width, _height);
090                    }
091                    else {
092                            renderedImage = imageTool.scale(renderedImage, _width);
093                    }
094    
095                    outputFile.createNewFile();
096    
097                    ImageIO.write(renderedImage, extension, outputFile);
098            }
099    
100            private final int _dpi;
101            private final String _extension;
102            private final boolean _generatePreview;
103            private final boolean _generateThumbnail;
104            private final int _height;
105            private final File _inputFile;
106            private final File[] _previewFiles;
107            private final String _thumbnailExtension;
108            private final File _thumbnailFile;
109            private final int _width;
110    
111    }