001/*
002 * (C) Copyright 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 *     Thibaud Arguillere
018 *     Miguel Nixo
019 */
020package org.nuxeo.ecm.platform.pdf.operations;
021
022import java.io.IOException;
023import org.apache.pdfbox.exceptions.COSVisitorException;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.util.BlobList;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.platform.pdf.PDFMerge;
034
035/**
036 * Merges Blobs into a single PDF.
037 * <p>
038 * The blob(s) in input will always be the first blob(s), to which we append:
039 * (1) blobToAppendVarName, if used, then (2) toAppendListVarName if used.
040 *
041 * @since 8.10
042 */
043@Operation(id = PDFMergeBlobsOperation.ID, category = Constants.CAT_CONVERSION, label = "PDF: Merge with Blob(s) ",
044    description = "The input blob(s) always is(are) the first PDFs. The operation appends the blob referenced in " +
045        "the <code>toAppendVarName</code> Context variable. It then appends all the blobs stored in the " +
046        "<code>toAppendListVarName</code> Context variable. Returns the final PDF.")
047public class PDFMergeBlobsOperation {
048
049    public static final String ID = "PDF.MergeWithBlobs";
050
051    @Context
052    protected OperationContext ctx;
053
054    @Param(name = "toAppendVarName", required = false)
055    protected String toAppendVarName = "";
056
057    @Param(name = "toAppendListVarName", required = false)
058    protected String toAppendListVarName = "";
059
060    @Param(name = "fileName", required = false)
061    protected String fileName = "";
062
063    @Param(name = "pdfTitle", required = false)
064    protected String pdfTitle = "";
065
066    @Param(name = "pdfSubject", required = false)
067    protected String pdfSubject = "";
068
069    @Param(name = "pdfAuthor", required = false)
070    protected String pdfAuthor = "";
071
072    @OperationMethod
073    public Blob run(Blob inBlob) throws NuxeoException {
074        PDFMerge pdfm = new PDFMerge(inBlob);
075        return doMerge(pdfm);
076    }
077
078    @OperationMethod
079    public Blob run(BlobList inBlobs) throws NuxeoException {
080        PDFMerge pdfm = new PDFMerge(inBlobs.get(0));
081        int max = inBlobs.size();
082        for (int i = 1; i < max; i++) {
083            pdfm.addBlob(inBlobs.get(i));
084        }
085        return doMerge(pdfm);
086    }
087
088    protected Blob doMerge(PDFMerge inMergeTool) throws NuxeoException {
089        // The first blob(s) has(have) already been added
090        // Append the single blob
091        if (toAppendVarName != null && !toAppendVarName.isEmpty()) {
092            inMergeTool.addBlob((Blob) ctx.get(toAppendVarName));
093        }
094        // Append the blob list
095        if (toAppendListVarName != null && !toAppendListVarName.isEmpty()) {
096            if (ctx.get(toAppendListVarName) instanceof BlobList) {
097                inMergeTool.addBlobs((BlobList) ctx.get(toAppendListVarName));
098            } else {
099                throw new NuxeoException(
100                    ctx.get(toAppendListVarName).getClass() + " is not a Collection");
101            }
102        }
103        // Merge
104        try {
105            return inMergeTool.merge(fileName, pdfTitle, pdfSubject, pdfAuthor);
106        } catch (COSVisitorException | IOException e) {
107            throw new NuxeoException(e);
108        }
109    }
110
111}