1

我想將電子表格轉換與水印/背景圖片的PDF,併發送+保存生成的PDF 的轉換爲PDF的工作,但我不知道是否/如何能將圖像放入生成的pdf中。谷歌電子表格與在谷歌腳本水印

這是我現在得到:

function ExportAndSent(subject, filename, email) { 
 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 
    var message = "A message"; 
 
    
 
    var tempSpreadsheet = SpreadsheetApp.create(filename); 
 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
 
    sheet = ss.getActiveSheet(); 
 
    sheet.copyTo(tempSpreadsheet); 
 
    
 
    tempSpreadsheet.deleteActiveSheet(); 
 
    
 
    var pdf = DriveApp.getFileById(tempSpreadsheet.getId()).getAs(MimeType.PDF); 
 
    var pdfBytes = pdf.getBytes(); 
 
    var attach = {fileName:(filename + ".pdf"),content:pdfBytes, mimeType:MimeType.PDF}; 
 
    
 
    // Here we need to put a watermark 
 
    
 
    // Send and export 
 
    MailApp.sendEmail(email, subject, message, {attachments:[attach]}); 
 
    DriveApp.createFile(pdf); 
 
    
 
    // Delete Temporary 
 
    DriveApp.getFileById(tempSpreadsheet.getId()).setTrashed(true); 
 
}

回答

0

它看起來像電子表格已經被轉換爲PDF。可以使用第三方服務添加水印來打開PDF併爲其添加水印。 PDF WebAPI是您可以使用的免費服務。下面我彙集了一個可以接受PDF併爲其添加水印的功能。水印當前被硬編碼爲「watermark.jpg」,但可以改變。從上面的代碼看,函數應該以下行之間被調用:

var pdf = DriveApp.getFileById(tempSpreadsheet.getId()).getAs(MimeType.PDF); 
 
var pdfBytes = pdf.getBytes();

的「PDF」變量應被用作輸入,並且「pdfBytes」,可以採取的返回appendWatermark()的值。

您需要註冊一個免費賬戶PDF WebAPI獲得一個ID和密鑰。

function appendWatermark(inputPDF) { 
 

 
    var fileBlob = inputPDF.copyBlob(); 
 

 
    var decorationData = [{ 
 
    "watermarkSettings": { 
 
     "opacity": 50, 
 
     "source": { 
 
     "file": "watermark.jpg", 
 
     "page": 1 
 
     }, 
 
     "scale": { 
 
     "type": "relative", 
 
     "percent": 90 
 
     }, 
 
     "location": "top", 
 
     "rotation": "0" 
 
    } 
 
    }]; 
 

 
    var applicationData = { 
 
    "id": "", 
 
    "key": "" 
 
    }; 
 

 
    var payload = { 
 
    "application": JSON.stringify(applicationData), 
 
    "input": fileBlob, 
 
    "decorationData": JSON.stringify(decorationData), 
 
    "resource": DriveApp.getFilesByName("watermark.jpg").next().getBlob() 
 
    }; 
 

 
    var options = { 
 
    "method": "post", 
 
    "payload": payload 
 
    }; 
 

 
    var response = UrlFetchApp.fetch("https://pdfprocess.datalogics.com/api/actions/decorate/document", options); 
 

 
    return response.getBytes; 
 
}

+0

很多很多的感謝! –

相關問題