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