Dom
Dom

Reputation: 453

Google App Script create a file PDF from document template base

In relation to my post:

Google app script on how to create a pdf from the data received from an html form of its spreadsheet and send the pdf file via email

In relation to the function described below:

function submitData(form) {
  var subject='New Feedback';
  var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment);

  var folderId = "my-folder-ID"; // Please set the folder ID.  // Added
  var blob = Utilities.newBlob(body, MimeType.HTML, form.name).getAs(MimeType.PDF);  // Added
  var file = DriveApp.getFolderById(folderId).createFile(blob);  // Added

  var aliases = GmailApp.getAliases()
   Logger.log(aliases); //returns the list of aliases you own
   Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array

  GmailApp.sendEmail('[email protected]','From an alias', 'A message from an alias!', {'from': aliases[0],subject: subject,htmlBody: body, attachments: [blob]});  // Modified

//  return file.getUrl();  // Modified
  return Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s<br />PDF: %s', form.name,form.email,form.comment,file.getUrl());
}

I would like use a document as a base template for creating a PDF file similar to this one: https://jeffreyeverhart.com/2018/09/17/auto-fill-google-doc-from-google-form-submission/ How can I accomplish this with my application? Which is a custom html form as you can see from above.

Thanks in advance for any help!

Upvotes: 1

Views: 1151

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to create a PDF file using the template of Google Document.
  • You want to set form.name,form.email,form.comment the template Document, and then, you want to export it as the PDF file.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • At first, please prepare a Google Document which is used as the template.
  • When there is the template Document, at first, copy the template Document and replace the values for replacing with form.name,form.email,form.comment. Then, the Document is converted to the PDF data.

Usage:

Before you run the script, please prepare a Google Document which is used as the template.

In this case, {{name}}, {{email}} and {{comment}} are replaced with form.name, form.email and form.comment, respectively. So please include {{name}}, {{email}} and {{comment}} in the template Document.

Modified script:

When your script is modified, it becomes as follows. And please set the file ID of the template Google Document to templateDocumentId.

From:
var blob = Utilities.newBlob(body, MimeType.HTML, form.name).getAs(MimeType.PDF);
To:
var templateDocumentId = "###"; // Please set the file ID of the template Google Document

var docId = DriveApp.getFileById(templateDocumentId).makeCopy("temp").getId();
var doc = DocumentApp.openById(docId);

// Please modify as follows.
// var body = doc.getBody();
// body.replaceText("{{name}}", form.name).replaceText("{{email}}", form.email).replaceText("{{comment}}", form.comment);
doc.getBody().replaceText("{{name}}", form.name).replaceText("{{email}}", form.email).replaceText("{{comment}}", form.comment); // Modified

doc.saveAndClose();
var blob = doc.getBlob().setName(form.name);
DriveApp.getFileById(docId).setTrashed(true);
  • In this modified script, the copied Document is moved to the trash box by the last line. So please be careful this.

Note:

  • I'm not sure about the template Document you want to use. So if this modified script cannot be used for your template Document, can you provide it?

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Upvotes: 3

Related Questions