Google Google 脚本中带水印的电子表格到 pdf
Google Spreadsheet to pdf with watermark in Google script
我想用 watermark/background 图片转换电子表格,然后发送并保存生成的 pdf
转换为 pdf 有效,但我不知道 if/how 您可以将图像放入生成的 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",但可以更改。从上面的代码来看,应该在以下几行之间调用该函数:
var pdf = DriveApp.getFileById(tempSpreadsheet.getId()).getAs(MimeType.PDF);
var pdfBytes = pdf.getBytes();
应该使用"pdf"变量作为输入,"pdfBytes",可以取appendWatermark()的return值。
您需要注册一个免费的 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;
}
我想用 watermark/background 图片转换电子表格,然后发送并保存生成的 pdf 转换为 pdf 有效,但我不知道 if/how 您可以将图像放入生成的 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",但可以更改。从上面的代码来看,应该在以下几行之间调用该函数:
var pdf = DriveApp.getFileById(tempSpreadsheet.getId()).getAs(MimeType.PDF);
var pdfBytes = pdf.getBytes();
应该使用"pdf"变量作为输入,"pdfBytes",可以取appendWatermark()的return值。
您需要注册一个免费的 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;
}