0
votes

I'm pretty new to programming and don't really know Javascript. Recently, to help out my mother with her business, I've came up with the script below. Basically it takes information from a sheet (the sheet is filled with answers from Google Forms), copy a template document and then replace some fields in the copied document with those informations.

Now what I'm trying to do is to export the filled document to PDF, can someone help?

Here's the actual script:

function autoFillGoogleDocFromForm(e) {
//e.values is an array of form values

var timestamp = e.values[0];
  
//client info
var nome = e.values[1];
var cpf = e.values[2];
var rg = e.values[3];
var rua = e.values[4];
var numero = e.values[5];
var bairro = e.values[6];
var cidade = e.values[7];
var estado = e.values[8];
var cep = e.values[9];
var celular = e.values[10];
  
  

//seller info
  var nome_vendedor = e.values[11];
  var cpf_vendedor = e.values[12];
  
  //template is the template file, and you get it by ID
  var template = DriveApp.getFileById('1VB5u4OrqIQO8P6scj-rzkjtRlZ8NI-ZKd9xGKfnEqBA'); 
  
  //acess main folder
  var mainfolder = DriveApp.getFolderById('1G6g2VyrvpKpqSvLei3_jefQMNsGb-be0');
  
  //create a new folder
  var newFolderId = mainfolder.createFolder(nome + ' ' + cpf).getId();
  
  //get new folder Id
  var id = DriveApp.getFolderById(newFolderId);
  
  //copy the template file to the new folder
  var copy = template.makeCopy(nome + ' ' + cpf, id);
  
  //open the document by it's Id
  var document = DocumentApp.openById(copy.getId()); 
  
  //get to the doc body for the replace
  var body = document.getBody();  
  
  
  ////////////
  ////////////
  ////////////
  ////////////
  ////////////
  //ReplaceText methods
  ////////////
  ////////////
  ////////////
  ////////////  
  
  
  //client info
  body.replaceText('{{nome}}', nome); 
  body.replaceText('{{cpf}}', cpf);  
  body.replaceText('{{rg}}', rg);
  body.replaceText('{{rua}}', rua); 
  body.replaceText('{{numero}}', numero);
  body.replaceText('{{bairro}}', bairro); 
  body.replaceText('{{cidade}}', cidade); 
  body.replaceText('{{estado}}', estado); 
  body.replaceText('{{cep}}', cep); 
  body.replaceText('{{celular}}', celular); 
  
  
  //seller info
  body.replaceText('{{nome_vendedor}}', nome_vendedor);
  body.replaceText('{{cpf_vendedor}}', cpf_vendedor);
  
    //save and close the document
    document.saveAndClose(); 
  
    }

    
1

1 Answers

0
votes

The easiest way to export a Goolge Document to pdf is using the UrlFetchApp

You need the

  • basic export url
  • the file id
  • specofy export parameters if and as required
  • An access token that you can obtain with the ScriptApp

Sample snippet based on you code before where you already have the document id:


  var url = "https://docs.google.com/document/d/"+id+"/export?";
  
  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                                   // paper size legal / letter / A4
  + '&portrait=true'                                // orientation, false for landscape
  +'&top_margin=0.50'
  +'&bottom_margin=0.50' 
  +'&left_margin=0.50' 
  +'&right_margin=0.50'
  // other parameters if you need
  /*
  + '&fitw=true&source=labnol'                       // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false'             // hide optional headers and footers
  + '&pagenumbers=false'                               // hide page numbers 
  + '&fzr=false'                                     // do not repeat row headers (frozen rows) on each page
  + '&gid=';                                         // the sheet's Id
  */
  
  var response = UrlFetchApp.fetch(url + url_ext, 
                                   {
                                   headers: {
                                   'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
                                   },
                                   muteHttpExceptions:true
                                   });
  DriveApp.createFile(response.getBlob().setName("myPdf"));