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.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentModelList; 035import org.nuxeo.ecm.platform.pdf.PDFMerge; 036 037/** 038 * The input document(s) always is(are) the first PDF(s), and each pdf is read in the <code>xpath</code> field. If 039 * <code>xpath</code> is not set, it is set to the default value <code>file:content</code>. 040 * <p> 041 * The operation appends: 042 * <ul> 043 * <li>First, The blob referenced in the <code>toAppendVarName</code> Context variable.</li> 044 * <li>Then, it appends all the blobs stored in the <code>toAppendListVarName</code> Context variable.</li> 045 * <li>And last, it appends the blobs stored in the docs whose IDs are passed in <code>toAppendDocIDsVarName</code>, 046 * using the <code>xpath</code> parameter.</li> 047 * </ul> 048 * <p> 049 * All variable names are optional: You can pass only <code>toAppendVarName</code>, or <code>toAppendVarName</code> and 050 * <code>toAppendDocIDsVarName</code>, or ... 051 * <p> 052 * Returns the final PDF. 053 * 054 * @since 8.10 055 */ 056@Operation(id = PDFMergeDocumentsOperation.ID, category = Constants.CAT_CONVERSION, 057 label = "PDF: Merge with Document(s)", description = "The input document(s) always is(are) the first PDFs, and " + 058 "their PDF is read in the <code>xpath</code> field (but it is ok for the input doc to have no blob). The " + 059 "operation appends the blob referenced in the <code>toAppendVarName</code> Context variable. It then appends all " + 060 "the blobs stored in the <code>toAppendListVarName</code> Context variable. It then append the blobs stored in " + 061 "the docs whose IDs are passed in <code>toAppendDocIDsVarName</code> (the same <code>xpath</code> is used). " + 062 "Returns the final PDF.") 063public class PDFMergeDocumentsOperation { 064 065 public static final String ID = "PDF.MergeWithDocs"; 066 067 @Context 068 protected CoreSession session; 069 070 @Context 071 protected OperationContext ctx; 072 073 @Param(name = "xpath", required = false, values = { "file:content" }) 074 protected String xpath = "file:content"; 075 076 @Param(name = "toAppendVarName", required = false) 077 protected String toAppendVarName = ""; 078 079 @Param(name = "toAppendListVarName", required = false) 080 protected String toAppendListVarName = ""; 081 082 @Param(name = "toAppendDocIDsVarName", required = false) 083 protected String toAppendDocIDsVarName = ""; 084 085 @Param(name = "fileName", required = false) 086 protected String fileName = ""; 087 088 @Param(name = "pdfTitle", required = false) 089 protected String pdfTitle = ""; 090 091 @Param(name = "pdfSubject", required = false) 092 protected String pdfSubject = ""; 093 094 @Param(name = "pdfAuthor", required = false) 095 protected String pdfAuthor = ""; 096 097 @OperationMethod 098 public Blob run(DocumentModel inDoc) throws NuxeoException { 099 PDFMerge pdfm = new PDFMerge(inDoc, xpath); 100 return doMerge(pdfm); 101 } 102 103 @OperationMethod 104 public Blob run(DocumentModelList inDocs) throws NuxeoException { 105 PDFMerge pdfm = new PDFMerge(inDocs, xpath); 106 return doMerge(pdfm); 107 } 108 109 protected Blob doMerge(PDFMerge inMergeTool) throws NuxeoException { 110 // Append the single blob 111 if (toAppendVarName != null && !toAppendVarName.isEmpty()) { 112 inMergeTool.addBlob((Blob) ctx.get(toAppendVarName)); 113 } 114 // Append the blob list 115 if (toAppendListVarName != null && !toAppendListVarName.isEmpty()) { 116 if (ctx.get(toAppendListVarName) instanceof BlobList) { 117 inMergeTool.addBlobs((BlobList) ctx.get(toAppendListVarName)); 118 } else { 119 throw new NuxeoException(ctx.get(toAppendListVarName).getClass() + " is not a Collection"); 120 } 121 } 122 // Append a list of Documents via their IDs 123 if (toAppendDocIDsVarName != null && !toAppendDocIDsVarName.isEmpty()) { 124 if (ctx.get(toAppendDocIDsVarName) instanceof String[]) { 125 inMergeTool.addBlobs((String[]) ctx.get(toAppendDocIDsVarName), xpath, session); 126 } else { 127 throw new NuxeoException(ctx.get(toAppendDocIDsVarName).getClass() + " is not a String[]"); 128 } 129 } 130 // Merge 131 try { 132 return inMergeTool.merge(fileName, pdfTitle, pdfSubject, pdfAuthor); 133 } catch (IOException e) { 134 throw new NuxeoException(e); 135 } 136 } 137 138}