Home > Software design >  Auto-Generate PDF with Image (actual image, not link) and Data Input in Google Form
Auto-Generate PDF with Image (actual image, not link) and Data Input in Google Form

Time:10-13

I'm creating a google form one can fill out to auto-generate a Digital Millennium Copyright Act notice as a PDF. The notice requires the original image in question to be included, so the form includes an upload file question. I want the generated PDF to include the actual image that was uploaded in the form, but right now all I get is the google drive link to the file's location.

How can I get the actual image to appear in the PDF? I'm attaching a screenshot of the Google Form, email output, and PDF template (marked with where the image should go) for reference.

google form email template

Here's the script I have on the Google Sheet that's populated by the Google Form submissions:

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

CodePudding user response:

Assuming that {{AttachOrig}} si your palceholder and AttachOrig the URL of the file

You need to:

  • Obtain the image blob from the URL by opening it with DriveApp and getting the blob
  • Find the palceholder in your template
  • Replace the text by an empty string
  • Find the element that contains the placeholder
  • Get the element's parent paragraph
  • Insert the image blob into the parent paragraph

Sample:

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 placerholder = "{{AttachOrig}}";
    var id = AttachOrig.match(/[\w\_\-]{25,}/)[0];
    var img = DriveApp.getFileById(id);
    var position = body.findText(placerholder);
    var element = position.getElement();
    element.asText().setText("");
    element.getParent().asParagraph().insertInlineImage(0, img.getBlob());
}
  • Related