001/*
002 * (C) Copyright 2012-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     ldoguin
016 */
017package org.nuxeo.template.deckjs;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.Serializable;
023import java.nio.file.Files;
024import java.nio.file.StandardCopyOption;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.Blobs;
029import org.nuxeo.ecm.core.api.CloseableFile;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.convert.api.ConversionException;
032import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
033import org.nuxeo.ecm.core.convert.extension.Converter;
034import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
035import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
036import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability;
037import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
038import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
039import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
040import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
041import org.nuxeo.runtime.api.Framework;
042
043public class DeckJSPDFConverter implements Converter {
044
045    @Override
046    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
047        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
048        CommandAvailability commandAvailability = cles.getCommandAvailability(DeckJSConverterConstants.PHANTOM_JS_COMMAND_NAME);
049        if (!commandAvailability.isAvailable()) {
050            return null;
051        }
052        Blob blob = blobHolder.getBlob();
053        File jsFile = null;
054        try (CloseableFile inputFile = blob.getCloseableFile(".html")) {
055            jsFile = File.createTempFile("phantomJsScript", ".js");
056            try (InputStream is = TemplateBundleActivator.getResourceAsStream(DeckJSConverterConstants.DECK_JS2PDF_JS_SCRIPT_PATH)) {
057                Files.copy(is, jsFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
058            }
059            Blob pdfOutput = Blobs.createBlobWithExtension(".pdf");
060            pdfOutput.setMimeType("application/pdf");
061            CmdParameters params = cles.getDefaultCmdParameters();
062            params.addNamedParameter("jsFilePath", jsFile);
063            params.addNamedParameter("inFilePath", inputFile.getFile());
064            params.addNamedParameter("outFilePath", pdfOutput.getFile());
065            ExecResult res = cles.execCommand("phantomjs", params);
066            if (!res.isSuccessful()) {
067                throw res.getError();
068            }
069            return new SimpleCachableBlobHolder(pdfOutput);
070        } catch (CommandNotAvailable | IOException | CommandException e) {
071            throw new ConversionException("PDF conversion failed", e);
072        } finally {
073            org.apache.commons.io.FileUtils.deleteQuietly(jsFile);
074        }
075    }
076
077    @Override
078    public void init(ConverterDescriptor descriptor) {
079    }
080
081}