5

I'm trying to work out how to create a PDF document through Google Apps Script which is displayed in landscape orientation (A4 size). This is the code I'm using to create the PDF so far, which comes out in portrait orientation.

function pdfSheet() {
  var d = new Date();
  var cdate = d.getDate();
  var cmonth = d.getMonth() + 1;
  var cyear = d.getFullYear();
  var current = [cdate + "-" + cmonth + "-" + cyear];

  var imageBlob = UrlFetchApp.fetch("https://sites.google.com/site/mysite/smalllogo.png").getBlob();
  var base64EncodedBytes = Utilities.base64Encode(imageBlob.getBytes());
  var logo = "<img src='data:image/png;base64," + base64EncodedBytes + "' width='170'/>";


  var html = "<table width='100%'><tr><td align='right'>" + logo + "<br><br><b>Date:</b> " + current + "</td></tr></table>"; //PDF content will carry on here.

  var gmailLabels  = "PDF";  
  var driveFolder  = "My Gmail";
  var folders = DriveApp.getFoldersByName(driveFolder);
  var folder = folders.hasNext() ? 
    folders.next() : DriveApp.createFolder(driveFolder);

  var subject = 'Test PDF';


  var tempFile = DriveApp.createFile("temp.html", html, "text/html");
  var page = folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf")
  tempFile.setTrashed(true); 

  var link = page.getUrl();
  Logger.log(link);

}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • I found a way to do it. If you are still interested, here is the link https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579. Works – nupac Dec 12 '14 at 08:02
  • See this thread: http://stackoverflow.com/questions/21997924/export-or-print-with-a-google-script-new-version-of-google-spreadsheets-to-pdf – Jim Garner Jan 24 '17 at 04:10
  • Was someone able to find out how to print a pdf horizontally or that doesn't work for pdfs? &portrait=false is not working. – kiki Apr 30 '19 at 22:17

1 Answers1

5

I took most of this from another user and edited to have the current days date (in EST) in the email subject and the PDF name. Hope it helps!

// Simple function to send Daily Status Sheets
// Load a menu item called "Project Admin" with a submenu item called "Send Status"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
  var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu);  
}

 function creatPDF() {
  SpreadsheetApp.flush();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //Date set with format in EST (NYC) used in subject and PDF name
  var period = Utilities.formatDate(new Date(), "GMT+5", "yyyy.MM.dd");
  var url = ss.getUrl();

  //remove the trailing 'edit' from the url
  url = url.replace(/edit$/, '');

  //additional parameters for exporting the sheet as a pdf
  var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf
    //below parameters are optional...
    '&size=letter' + //paper size
    '&portrait=false' + //orientation, false for landscape
    '&fitw=true' + //fit to width, false for actual size
    '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
    '&gridlines=false' + //hide gridlines
    '&fzr=false' + //do not repeat row headers (frozen rows) on each page
    '&gid=' + sheet.getSheetId(); //the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url + url_ext, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });

  var blob = response.getBlob().setName(ss.getName() + " " + period + '.pdf');

  //from here you should be able to use and manipulate the blob to send and email or create a file per usual.
   var email = 'email@co-email.com'; 
   var subject = "subject line " + period ;
    var body = "Please find attached your Daily Report.";
//Place receipient email between the marks
    MailApp.sendEmail( email, subject, body, {attachments:[blob]});


}
Luke Holcomb
  • 165
  • 1
  • 8