谷歌电子表格到谷歌脚本水印的pdf [英] Google Spreadsheet to pdf with watermark in Google script

查看:116
本文介绍了谷歌电子表格到谷歌脚本水印的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用水印/背景图像转换电子表格,并发送+保存生成的PDF
转换为PDF格式,但我不知道是否/如何将图像放入生成的pdf。



这就是我现在得到的:

函数ExportAndSent(subject,filename,email){var ss = SpreadsheetApp.getActiveSpreadsheet (); var 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}; //这里我们需要放一个水印//发送并导出MailApp.sendEmail(email,subject,message,{attachments:[attach]}); DriveApp.createFile(PDF); //删除Temporary DriveApp.getFileById(tempSpreadsheet.getId())。setTrashed(true);}

b

解决方案

它看起来像电子表格已经转换为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和密钥。



 函数appendWatermark(inputPDF){var fileBlob = inputPDF.copyBlob(); var decorationData = [{watermarkSettings:{opacity:50,source:{file:watermark.jpg,page:1},scale:{type:relative ,百分比:90},位置:顶部,旋转: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;}  


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.

This is what i got now:

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);
}

解决方案

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();

The "pdf" variable should be used as input, and "pdfBytes", can take the return value of appendWatermark().

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆