001/*
002 * (C) Copyright 2013 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.pdfbox.exceptions.COSVisitorException;
025import org.apache.pdfbox.util.PDFMergerUtility;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.OperationException;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.automation.core.util.BlobList;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Given a File document holding a pdf on the file:content property and 2 pdfs on the files:files property, the
040 * following operation will provide a pdf that is the result of the merge of all the pdfs, with the content of the one
041 * in file:content property first.
042 *
043 * @since 5.8
044 */
045@Operation(id = ConcatenatePDFs.ID, category = Constants.CAT_CONVERSION, label = "Concatenate PDFs", description = "Given a File document holding a pdf on the file:content property and 2 pdfs on the files:files property, the following operation will provide a pdf that is the result of the merge of all the pdfs, with the content of the one in file:content property first.")
046public class ConcatenatePDFs {
047
048    public static final String ID = "Blob.ConcatenatePDFs";
049
050    @Context
051    protected OperationContext ctx;
052
053    @Param(name = "blob_to_append", required = false, description = "Optional blob reference in context to append in first place.")
054    protected String xpathBlobToAppend = "";
055
056    @Param(name = "filename", required = true, description = "The merge pdf result filename.")
057    protected String filename;
058
059    @OperationMethod
060    public Blob run(Blob blob) throws OperationException, IOException, COSVisitorException {
061        PDFMergerUtility ut = new PDFMergerUtility();
062        checkPdf(blob);
063        if (xpathBlobToAppend.isEmpty()) {
064            return blob;
065        }
066        handleBlobToAppend(ut);
067        ut.addSource(blob.getStream());
068        return appendPDFs(ut);
069    }
070
071    @OperationMethod
072    public Blob run(BlobList blobs) throws IOException, OperationException, COSVisitorException {
073        PDFMergerUtility ut = new PDFMergerUtility();
074        if (!xpathBlobToAppend.isEmpty()) {
075            handleBlobToAppend(ut);
076        }
077        for (Blob blob : blobs) {
078            checkPdf(blob);
079            ut.addSource(blob.getStream());
080        }
081        return appendPDFs(ut);
082    }
083
084    protected Blob appendPDFs(PDFMergerUtility ut) throws IOException, COSVisitorException {
085        File tempFile = Framework.createTempFile(filename, ".pdf");
086        ut.setDestinationFileName(tempFile.getAbsolutePath());
087        ut.mergeDocuments();
088        Blob fb = Blobs.createBlob(tempFile);
089        Framework.trackFile(tempFile, fb);
090        fb.setFilename(filename);
091        return fb;
092    }
093
094    /**
095     * Check if blob to append is a PDF blob.
096     */
097    protected void handleBlobToAppend(PDFMergerUtility ut) throws IOException, OperationException {
098        try {
099            Blob blobToAppend = (Blob) ctx.get(xpathBlobToAppend);
100            if (blobToAppend == null) {
101                throw new OperationException("The blob to append from variable context: '" + xpathBlobToAppend
102                        + "' is null.");
103            }
104            checkPdf(blobToAppend);
105            ut.addSource(blobToAppend.getStream());
106        } catch (ClassCastException e) {
107            throw new OperationException("The blob to append from variable context: '" + xpathBlobToAppend
108                    + "' is not a blob.", e);
109        }
110    }
111
112    /**
113     * Check if blob is a pdf.
114     */
115    protected void checkPdf(Blob blob) throws OperationException {
116        if (!"application/pdf".equals(blob.getMimeType())) {
117            throw new OperationException("Blob " + blob.getFilename() + " is not a PDF.");
118        }
119    }
120}