Mous
Mous

Reputation: 159

Specify PDF printing options within GAS

I'd like to know how can I specify in my code to print in Landscape mode and fit to letter sheet.

I found this code and I want to incorporate it to the maine code.

function printPdf() {
    SpreadsheetApp.flush();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var gid = sheet.getSheetId();
    var pdfOpts = '&size=A3&fzr=true&portrait=false&fitw=true&gridlines=false&printtitle=true&sheetnames=true&pagenumbers=true&attachment=false&gid='+gid;
    var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts
    var app = UiApp.createApplication().setWidth(300).setHeight(100);
    app.setTitle('Your Print Preview is Ready');
    var link = app.createAnchor('Open Print Preview', url).setTarget('_new');
    app.add(link);
    ss.show(app);
}

This is the main code:

function emailAsPDF() {
    var emailAddress=Session.getActiveUser().getEmail();
  var mess="Voulez-vous envoyer votre rapport  à l'adresse : " + emailAddress;
  var ans= Browser.msgBox("Courriel", mess, Browser.Buttons.YES_NO);
  if (ans===Browser.Buttons.NO){return;}
  var mess="Votre rapport a été envoyé à l'adresse : " + emailAddress;
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName("Recherche");
  var CandidateName=ss.getRangeByName("Nom.Candidat").getValue();
  var emailSubject="Vérifications pré-emploi complétées" +" - "+ CandidateName;
  var emailMessage="Bonjour," + "\n\n" + "J’ai le plaisir de vous informer que les vérifications sont complétées pour le candidat indiqué au tableau de résultats pré-emploi suivant:" + "\n\n" + "Bonne journée !";
  var shts=ss.getSheets();
  var hdnA=[];
  shts.forEach(function(sht){if(sht.getName()!="Recherche") {sht.hideSheet();hdnA.push(sht.getName());}})
  var blob = ss.getAs('application/pdf');
  var file=DriveApp.getFileById(ss.getId());
  var folder=file.getParents().next();
  var pdfName = ss.getName() + " - " + CandidateName + '.pdf';
  var pdfFile = folder.createFile(blob).setName(pdfName);
  MailApp.sendEmail(emailAddress, emailSubject, emailMessage,{attachments:[pdfFile]});
  DriveApp.getFileById(pdfFile.getId()).setTrashed(true);
  hdnA.forEach(function(name){ss.getSheetByName(name).showSheet();})
  Browser.msgBox("Courriel", mess, Browser.Buttons.OK); 
}

I expect to be able to modify my printing display: landscape, fit to the sheet leter...

Upvotes: 0

Views: 131

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

To incorporate function printPdf() into your existing code, keep the following in mind:

    var app = UiApp.createApplication().setWidth(300).setHeight(100);
    app.setTitle('Your Print Preview is Ready');
    var link = app.createAnchor('Open Print Preview', url).setTarget('_new');
    app.add(link);
    ss.show(app);

is no longer a valid part of the code.

  • function printPdf() gives you the url of the exported pdf, to obtain the pdf itself you need to use UrlFetchApp
  • UrlFetchApp.fetch() allows you toobtain the pdf file as a blob and directly incorporate the latter into the attachment - it is not necessary to save it first on your drive and then trash.

This is a sample code that permits you to attach your sheet as pdf in landscape to your email:

function emailAsPDF() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var gid = sheet.getSheetId();
 var pdfOpts = '&size=A3&fzr=true&portrait=false&fitw=true&gridlines=false&printtitle=true&sheetnames=true&pagenumbers=true&attachment=false&gid='+gid;
 var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts
 var options = {
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
      }
    }
  var blobresponse = UrlFetchApp.fetch(url, options);
  var blob=blobresponse.getBlob().setName(ss.getName() + " - " + CandidateName+".pdf" );
  var emailAddress=Session.getActiveUser().getEmail();
  var mess="Voulez-vous envoyer votre rapport  à l'adresse : " + emailAddress;
  var ans= Browser.msgBox("Courriel", mess, Browser.Buttons.YES_NO);
  if (ans===Browser.Buttons.NO){return;}
  var mess="Votre rapport a été envoyé à l'adresse : " + emailAddress;
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName("Recherche");
  var CandidateName=ss.getRangeByName("Nom.Candidat").getValue();
  var emailSubject="Vérifications pré-emploi complétées" +" - "+ CandidateName;
  var emailMessage="Bonjour," + "\n\n" + "J’ai le plaisir de vous informer que les vérifications sont complétées pour le candidat indiqué au tableau de résultats pré-emploi suivant:" + "\n\n" + "Bonne journée !";
  var shts=ss.getSheets();
  var hdnA=[];
  shts.forEach(function(sht){if(sht.getName()!="Recherche") {sht.hideSheet();hdnA.push(sht.getName());}})
  MailApp.sendEmail(emailAddress, emailSubject, emailMessage,{attachments:[blob]});
  hdnA.forEach(function(name){ss.getSheetByName(name).showSheet();})
  Browser.msgBox("Courriel", mess, Browser.Buttons.OK); 
}

Upvotes: 1

Related Questions