自动生成带有图像(实际图像,不是 link)和 Google 形式的数据输入的 PDF
Auto-Generate PDF with Image (actual image, not link) and Data Input in Google Form
我正在创建一个 google 表单,可以填写该表单以自动生成 PDF 格式的数字千年版权法案通知。该通知要求包含有问题的原始图像,因此该表格包含一个上传文件问题。我希望生成的 PDF 包含在表单中上传的实际图像,但现在我得到的只是 google 驱动器 link 到文件的位置。
如何让实际图像出现在 PDF 中?我附上了 Google 表单、电子邮件输出和 PDF 模板的屏幕截图(标有图像应放置的位置)以供参考。
这是我在 Google Sheet 上的脚本,由 Google 表单提交填充:
function onSubmit(e) {
const rg = e.range;
console.log(rg.getA1Notation());
const sh = rg.getSheet();
//Get all the form submitted data
//Note: This data is dependent on the headers. If headers, are changed update these as well.
const Email= e.namedValues['Email Address'][0];
const LinkOrig = e.namedValues['Link(s) for where the original work appears'][0];
const AttachOrig = e.namedValues['Copies of the original copyrighted work'][0];
const Domain = e.namedValues['Infringing Domain'][0];
const LinkInfring = e.namedValues['Link(s) for where infringing image appears online'][0];
const Contact = e.namedValues['Contact Information'][0];
const WHOIS = e.namedValues['WHOIS Search results'][0];
const Date = e.namedValues['Date'][0];
const Location = e.namedValues['Where are you based?'][0];
//Build a new DMCA Form from the template
//Folder ID (save destination) and file IDs (template ID + new doc ID)
const DMCAFolderID = 'folderidhere';
const DMCALibFolder = DriveApp.getFolderById(DMCAFolderID);
const TemplateFileID = 'templateidhere';
const newFilename = 'DMCA Notice -' + TemplateFileID + 'Domain';
//Make a copy of the template file
const newTemplateFileID = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, DMCALibFolder).getId();;
//Get the DMCA Notice body into a variable
var document = DocumentApp.openById(newTemplateFileID);
var body = document.getBody();
//Replace all the {{ }} text in the template body
body.replaceText('{{LinkOrig}}', LinkOrig);
body.replaceText('{{AttachOrig}}', AttachOrig);
body.replaceText('{{LinkInfring}}', LinkInfring);
body.replaceText('{{ContactInfo}}', Contact);
body.replaceText('{{WHOISResults}}', WHOIS);
body.replaceText('{{date}}', Date);
body.replaceText('{{location}}', Location);
document.saveAndClose();
// define email variables
var subject = 'DMCA Notice - ' + Domain;
var msgHtml =
"Hi," + "<br/>" + "<br/>" +
"Please find your DMCA Notice attached." + "<br/>" + "<br/>" +
"Sincerely," + "<br/>" +
"Your Bada** Self" + "<br/>"
;
var attachment = DriveApp.getFileById(newTemplateFileID);
//send email with the file
GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)]});
}```
假设 {{AttachOrig}}
是您的占位符,AttachOrig
是文件的 URL
您需要:
- 通过使用 DriveApp 打开并获取 blob,从 URL 获取图像 blob
- 在您的模板中找到占位符
- 用空字符串替换文本
- 查找包含占位符的元素
- 获取元素的父段落
- 将图像 blob 插入父段落
样本:
var AttachOrig = "https://drive.google.com/file/d/1f8EFG6G5zNd6by_fNEecSh2D1My_p-_p/view";
function replacePlaceHolderThroughImage() {
...
var document = DocumentApp.openById(newTemplateFileID);
var body = document.getBody();
var placeholder = "{{AttachOrig}}";
var id = AttachOrig.match(/[\w\_\-]{25,}/)[0];
var img = DriveApp.getFileById(id);
var position = body.findText(placeholder);
var element = position.getElement();
element.asText().setText("");
element.getParent().asParagraph().insertInlineImage(0, img.getBlob());
}
我正在创建一个 google 表单,可以填写该表单以自动生成 PDF 格式的数字千年版权法案通知。该通知要求包含有问题的原始图像,因此该表格包含一个上传文件问题。我希望生成的 PDF 包含在表单中上传的实际图像,但现在我得到的只是 google 驱动器 link 到文件的位置。
如何让实际图像出现在 PDF 中?我附上了 Google 表单、电子邮件输出和 PDF 模板的屏幕截图(标有图像应放置的位置)以供参考。
这是我在 Google Sheet 上的脚本,由 Google 表单提交填充:
function onSubmit(e) {
const rg = e.range;
console.log(rg.getA1Notation());
const sh = rg.getSheet();
//Get all the form submitted data
//Note: This data is dependent on the headers. If headers, are changed update these as well.
const Email= e.namedValues['Email Address'][0];
const LinkOrig = e.namedValues['Link(s) for where the original work appears'][0];
const AttachOrig = e.namedValues['Copies of the original copyrighted work'][0];
const Domain = e.namedValues['Infringing Domain'][0];
const LinkInfring = e.namedValues['Link(s) for where infringing image appears online'][0];
const Contact = e.namedValues['Contact Information'][0];
const WHOIS = e.namedValues['WHOIS Search results'][0];
const Date = e.namedValues['Date'][0];
const Location = e.namedValues['Where are you based?'][0];
//Build a new DMCA Form from the template
//Folder ID (save destination) and file IDs (template ID + new doc ID)
const DMCAFolderID = 'folderidhere';
const DMCALibFolder = DriveApp.getFolderById(DMCAFolderID);
const TemplateFileID = 'templateidhere';
const newFilename = 'DMCA Notice -' + TemplateFileID + 'Domain';
//Make a copy of the template file
const newTemplateFileID = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, DMCALibFolder).getId();;
//Get the DMCA Notice body into a variable
var document = DocumentApp.openById(newTemplateFileID);
var body = document.getBody();
//Replace all the {{ }} text in the template body
body.replaceText('{{LinkOrig}}', LinkOrig);
body.replaceText('{{AttachOrig}}', AttachOrig);
body.replaceText('{{LinkInfring}}', LinkInfring);
body.replaceText('{{ContactInfo}}', Contact);
body.replaceText('{{WHOISResults}}', WHOIS);
body.replaceText('{{date}}', Date);
body.replaceText('{{location}}', Location);
document.saveAndClose();
// define email variables
var subject = 'DMCA Notice - ' + Domain;
var msgHtml =
"Hi," + "<br/>" + "<br/>" +
"Please find your DMCA Notice attached." + "<br/>" + "<br/>" +
"Sincerely," + "<br/>" +
"Your Bada** Self" + "<br/>"
;
var attachment = DriveApp.getFileById(newTemplateFileID);
//send email with the file
GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)]});
}```
假设 {{AttachOrig}}
是您的占位符,AttachOrig
是文件的 URL
您需要:
- 通过使用 DriveApp 打开并获取 blob,从 URL 获取图像 blob
- 在您的模板中找到占位符
- 用空字符串替换文本
- 查找包含占位符的元素
- 获取元素的父段落
- 将图像 blob 插入父段落
样本:
var AttachOrig = "https://drive.google.com/file/d/1f8EFG6G5zNd6by_fNEecSh2D1My_p-_p/view";
function replacePlaceHolderThroughImage() {
...
var document = DocumentApp.openById(newTemplateFileID);
var body = document.getBody();
var placeholder = "{{AttachOrig}}";
var id = AttachOrig.match(/[\w\_\-]{25,}/)[0];
var img = DriveApp.getFileById(id);
var position = body.findText(placeholder);
var element = position.getElement();
element.asText().setText("");
element.getParent().asParagraph().insertInlineImage(0, img.getBlob());
}