简体   繁体   中英

Script to edit Google Doc then Convert to PDF

I've been trying to make a system where I change the content of a google doc then save it as a pdf. This script is running in a Google Sheet so I'm not sure if that's my issue. I can get to the point where it's edited with the right info, but it;s still a Doc, not a PDF. Also I don't code, so this is as far as I've gotten based on other people's work. Any help would be great, Thanks!

function autoFillGoogleDocFromForm(e) {
  //e.values is an array of form values
  var timestamp = e.values[0];
  var InvenName = e.values[1];
  var Case = e.values[2];
  var AppNumber = e.values[3];
  var Firm = e.values[4];
  var InvenTitle = e.values[5];
  var Date = e.values[6];
  var AppType = e.values[7];

  //file is the template file, and you get it by ID
  var file = DriveApp.getFileById('FILE_ID_HERE'); 

  //We can make a copy of the template, name it, and optionally tell it what folder to live in
  //file.makeCopy will return a Google Drive file object
  var folder = DriveApp.getFolderById('FOLDER_ID_HERE')
  var copy = file.makeCopy(Case + ' ' + 'E Assigmnt' + ' ' + AppType + ' ' + AppNumber + ' ' + InvenName ,folder);

  //Once we've got the new file created, we need to open it as a document by using its ID
  var doc = DocumentApp.openById(copy.getId()); 

  //Since everything we need to change is in the body, we need to get that
  var body = doc.getBody(); 
  var footer = doc.getFooter();

  //Then we call all of our replaceText methods
  body.replaceText('{{Name}}', InvenName);
  body.replaceText('{{Title}}' ,InvenTitle);
  body.replaceText('{{AppNumber}}', AppNumber);
  body.replaceText('{{Date}}' , Date);
  footer.replaceText('{{FirmNumber}}', Firm);
  footer.replaceText('{{Case}}', Case);

  //Lastly we save and close the document to persist our changes
  DocumentApp.getActiveDocument().saveAndClose();

  var doc = DocumentApp.getActiveDocument();
  var docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
  docblob.setName(doc.getName() + ".pdf");

}

I believe your goal as follows.

  • You want to create a PDF file by modifying the copied Google Document using Google Apps Script.

For this, how about this answer?

Modification points:

  • In your script, the copied Google Document is modified by replacing texts. But the modified Document is not exported as the PDF file. The active Document is converted to the PDF format. But, the blob is not exported as a file.

Modified script:

When your script is modified, please modify as follows.

From:
 DocumentApp.getActiveDocument().saveAndClose(); var doc = DocumentApp.getActiveDocument(); var docblob = DocumentApp.getActiveDocument().getAs('application/pdf'); docblob.setName(doc.getName() + ".pdf");
To:
 doc.saveAndClose(); var docblob = doc.getBlob(); docblob.setName(doc.getName() + ".pdf"); DriveApp.createFile(docblob);
  • By above modification, the copied Google Document is modified by replacing texts, and the modified Document is exported as a PDF file. In this case, the created PDF file is put to the root folder.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM