谷歌电子表格到 pdf 与谷歌脚本中的水印 [英] Google Spreadsheet to pdf with watermark in Google script
问题描述
我想用水印/背景图像转换电子表格,并发送+保存生成的pdf转换为 pdf 有效,但我不知道您是否/如何将图像放入生成的 pdf.
I want to convert the spreadsheet with a watermark/background image, and send + save the generated pdf The converting to pdf worked, but I don't know if/how you can put an image to the generated 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);
}
推荐答案
电子表格似乎已转换为 PDF.可以使用第三方服务添加水印以打开 PDF 并为其添加水印.PDF WebAPI 是您可以使用的免费服务.下面我整理了一个可以接收PDF并为其添加水印的功能.水印目前被硬编码为watermark.jpg",但可以更改.从上面的代码来看,应该在以下几行之间调用该函数:
It looks like the spreadsheet is already converted to a PDF. A third party service can be used to add a watermark to open the PDF and add a watermark to it. PDF WebAPI is a free service you can use. Below I put together a function that can take in a PDF and add a watermark to it. The watermark is hardcoded to "watermark.jpg" currently, but that can be changed. From looking at the code above, the function should be called between the following lines:
var pdf = DriveApp.getFileById(tempSpreadsheet.getId()).getAs(MimeType.PDF);
var pdfBytes = pdf.getBytes();
应该使用pdf"变量作为输入,而pdfBytes"可以作为appendWatermark()的返回值.
The "pdf" variable should be used as input, and "pdfBytes", can take the return value of appendWatermark().
您需要注册一个免费的PDF WebAPI 帐户才能获得ID 和密钥.
You will need to sign up for a free PDF WebAPI account to get an ID and key.
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;
}
这篇关于谷歌电子表格到 pdf 与谷歌脚本中的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!