001/*
002 * (C) Copyright 2006-2016 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 *     Tiry
018 *     bstefanescu <bs@nuxeo.com>
019 *     Estelle Giuly <egiuly@nuxeo.com>
020 */
021package org.nuxeo.ecm.automation.core.operations.blob;
022
023import java.io.IOException;
024import java.util.Collections;
025
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.util.BlobList;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
035import org.nuxeo.ecm.core.convert.api.ConversionService;
036import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
037
038/**
039 * Save the input document
040 */
041@Operation(id = BlobToPDF.ID, category = Constants.CAT_CONVERSION, label = "Convert To PDF", description = "Convert the input file to a PDF and return the new file.")
042public class BlobToPDF {
043
044    public static final String ID = "Blob.ToPDF";
045
046    @Context
047    protected ConversionService service;
048
049    @OperationMethod
050    public Blob run(DocumentModel doc) throws IOException {
051        BlobHolder bh = doc.getAdapter(BlobHolder.class);
052        if (bh == null) {
053            return null;
054        }
055        return service.convertToMimeType(MimetypeRegistry.PDF_MIMETYPE, bh, Collections.emptyMap()).getBlob();
056    }
057
058    @OperationMethod
059    public Blob run(Blob blob) throws IOException {
060        return service.convertToMimeType(MimetypeRegistry.PDF_MIMETYPE, new SimpleBlobHolder(blob),
061                Collections.emptyMap()).getBlob();
062    }
063
064    @OperationMethod
065    public BlobList run(BlobList blobs) throws IOException {
066        BlobList bl = new BlobList();
067        for (Blob blob : blobs) {
068            bl.add(this.run(blob));
069        }
070        return bl;
071    }
072
073}