Skip to content Skip to sidebar Skip to footer

Updated Doc Not Attaching As Pdf In Gmail

I'm using Google Apps Script to create a copy of a doc file and save data from spreadsheet to the new file, then send it to email as a PDF attachment. Everything is working as it s

Solution 1:

I think, it can be done if we consider the 'files' as 'blobs', that is how Google treats the Google Docs files. Please see this example which converts and sends Google Spreadsheets as PDF files.

/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email addressvaremail="amit@labnol.org"; 

  // Subject of email message// The date time string can be formatted in your timezone using Utilities.formatDate methodvarsubject="PDF Reports - " + (newDate()).toString(); 

  // Get the currently active spreadsheet URL (link)// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");varss= SpreadsheetApp.getActiveSpreadsheet();

  // Email Body can  be HTML too with your logo image - see ctrlq.org/html-mailvarbody="PDF generated using code at ctrlq.org from sheet " + ss.getName(); 

  varurl= ss.getUrl();
  url = url.replace(/edit$/,'');

  /* Specify PDF export parameters
  // From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
    exportFormat = pdf / csv / xls / xlsx
    gridlines = true / false
    printtitle = true (1) / false (0)
    size = legal / letter/ A4
    fzr (repeat frozen rows) = true / false
    portrait = true (1) / false (0)
    fitw (fit to page width) = true (1) / false (0)
    add gid if to export a particular sheet - 0, 1, 2,..
  */varurl_ext='export?exportFormat=pdf&format=pdf'// export as pdf
                + '&size=letter'// paper size
                + '&portrait=false'// orientation, false for landscape
                + '&fitw=true&source=labnol'// fit to width, false for actual size
                + '&sheetnames=false&printtitle=false'// hide optional headers and footers
                + '&pagenumbers=false&gridlines=false'// hide page numbers and gridlines
                + '&fzr=false'// do not repeat row headers (frozen rows) on each page
                + '&gid=';                             // the sheet's Idvartoken= ScriptApp.getOAuthToken();
  varsheets= ss.getSheets(); 

  //make an empty array to hold your fetched blobs  varblobs= [];

  for (var i=0; i<sheets.length; i++) {

    // Convert individual worksheets to PDFvarresponse= UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
    blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');

  }

  //create new blob that is a zip file containing our blob arrayvarzipBlob= Utilities.zip(blobs).setName(ss.getName() + '.zip'); 

  //optional: save the file to the root folder of Google Drive
  DriveApp.createFile(zipBlob);

  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

  // If allowed to send emails, send the email with the PDF attachmentif (MailApp.getRemainingDailyQuota() > 0) 
     GmailApp.sendEmail(email, subject, body, {attachments:[zipBlob]});

}

More in here: Convert and Email Google Spreadsheets as PDF Files

Post a Comment for "Updated Doc Not Attaching As Pdf In Gmail"