001/* 002 * (C) Copyright 2012-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * ldoguin 018 */ 019package org.nuxeo.template.deckjs; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.Serializable; 025import java.nio.file.Files; 026import java.nio.file.StandardCopyOption; 027import java.util.Map; 028 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.Blobs; 031import org.nuxeo.ecm.core.api.CloseableFile; 032import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 033import org.nuxeo.ecm.core.convert.api.ConversionException; 034import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 035import org.nuxeo.ecm.core.convert.extension.Converter; 036import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 037import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters; 038import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability; 039import org.nuxeo.ecm.platform.commandline.executor.api.CommandException; 040import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService; 041import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable; 042import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult; 043import org.nuxeo.runtime.api.Framework; 044 045public class DeckJSPDFConverter implements Converter { 046 047 @Override 048 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 049 CommandLineExecutorService cles = Framework.getService(CommandLineExecutorService.class); 050 CommandAvailability commandAvailability = cles.getCommandAvailability(DeckJSConverterConstants.PHANTOM_JS_COMMAND_NAME); 051 if (!commandAvailability.isAvailable()) { 052 return null; 053 } 054 Blob blob = blobHolder.getBlob(); 055 File jsFile = null; 056 try (CloseableFile inputFile = blob.getCloseableFile(".html")) { 057 jsFile = Framework.createTempFile("phantomJsScript", ".js"); 058 try (InputStream is = TemplateBundleActivator.getResourceAsStream(DeckJSConverterConstants.DECK_JS2PDF_JS_SCRIPT_PATH)) { 059 Files.copy(is, jsFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 060 } 061 Blob pdfOutput = Blobs.createBlobWithExtension(".pdf"); 062 pdfOutput.setMimeType("application/pdf"); 063 CmdParameters params = cles.getDefaultCmdParameters(); 064 params.addNamedParameter("jsFilePath", jsFile); 065 params.addNamedParameter("inFilePath", inputFile.getFile()); 066 params.addNamedParameter("outFilePath", pdfOutput.getFile()); 067 ExecResult res = cles.execCommand("phantomjs", params); 068 if (!res.isSuccessful()) { 069 throw res.getError(); 070 } 071 return new SimpleCachableBlobHolder(pdfOutput); 072 } catch (CommandNotAvailable | IOException | CommandException e) { 073 throw new ConversionException("PDF conversion failed", e); 074 } finally { 075 org.apache.commons.io.FileUtils.deleteQuietly(jsFile); 076 } 077 } 078 079 @Override 080 public void init(ConverterDescriptor descriptor) { 081 } 082 083}