ledisnomad
ledisnomad

Reputation: 37

How to convert all Google Docs in a folder into PDFs?

I've created a simple Google Apps script to convert all Google Documents in a folder in my Drive into PDFs and save them to the same folder, which works:

function convertDocsToPDF() {
 
  var folderId = "asdf";
  var folder = DriveApp.getFolderById("folderId");
  var invoices = folder.getFiles();
  
  while (invoices.hasNext()) {
    var invoice = invoices.next();
    var id = invoice.getId();
    var file = DriveApp.getFileById(id);
    var PDFblob = file.getAs(MimeType.PDF);
    var PDF = DriveApp.createFile(PDFblob);
    PDF.moveTo(file.getParents().next());
  }
}

I wrote this to test out the conversion, but want to integrate it into a longer script. The longer script creates the invoices from a template by replacing ##TextToReplace## styled fields with data I pull from spreadsheets.

When I try to integrate the convertDocsToPDF() functionality, the PDFs all look like the template, not the invoice. When I look at the invoice document when the script is done, it looks correct with all the fields replaced with the correct data, but the PDF just looks like a copy of the template.

Is there a lag between when my script edits the invoice and when it is saved in my Drive such that the PDF conversion is seeing the invoice before the replaced text?

Upvotes: 2

Views: 699

Answers (1)

iansedano
iansedano

Reputation: 6481

It takes time for an edit to be pushed to the server.

This delay may be very small, but a script will be faster.

Things you can try:

Upvotes: 3

Related Questions