Generate PDFs from Google Sheets Using Apps Script
Generating PDFs from a spreadsheet — invoices, reports, timesheets — is a common requirement that Apps Script handles well. You can export the whole sheet, a specific range, or a custom-styled version.
Export the active sheet as a PDF
functionexportSheetAsPdf(){const ss =SpreadsheetApp.getActiveSpreadsheet();const sheetId = ss.getActiveSheet().getSheetId();const ssId = ss.getId();const url =`https://docs.google.com/spreadsheets/d/${ssId}/export`+`?format=pdf`+`&size=A4`+`&portrait=true`+`&fitw=true`+// Fit to width`&sheetnames=false`+`&printtitle=false`+`&pagenumbers=false`+`&gridlines=false`+`&fzr=false`+// Don't repeat frozen rows`&gid=${sheetId}`;const token =ScriptApp.getOAuthToken();const response =UrlFetchApp.fetch(url,{headers:{Authorization:'Bearer '+ token },});const pdfBlob = response.getBlob().setName(ss.getName()+'.pdf');// Save to Driveconst file =DriveApp.createFile(pdfBlob);Logger.log('PDF saved: '+ file.getUrl());}
Export a specific range as PDF
functionexportRangeAsPdf(){const ss =SpreadsheetApp.getActiveSpreadsheet();const sheet = ss.getActiveSheet();const sheetId = sheet.getSheetId();const ssId = ss.getId();// Define range (row/column are 1-indexed, converted to 0-indexed for URL)const range = sheet.getRange('A1:F20');const startRow = range.getRow()-1;const startCol = range.getColumn()-1;const endRow = startRow + range.getNumRows();const endCol = startCol + range.getNumColumns();const url =`https://docs.google.com/spreadsheets/d/${ssId}/export`+`?format=pdf`+`&size=A4`+`&portrait=true`+`&fitw=true`+`&gridlines=false`+`&gid=${sheetId}`+`&r1=${startRow}&c1=${startCol}&r2=${endRow}&c2=${endCol}`;const token =ScriptApp.getOAuthToken();const blob =UrlFetchApp.fetch(url,{headers:{Authorization:'Bearer '+ token },}).getBlob().setName('Report.pdf');DriveApp.createFile(blob);Logger.log('Range exported as PDF.');}
Email the PDF as an attachment
functionemailPdf(){const ss =SpreadsheetApp.getActiveSpreadsheet();const ssId = ss.getId();const sheetId = ss.getActiveSheet().getSheetId();const url =`https://docs.google.com/spreadsheets/d/${ssId}/export?format=pdf&gid=${sheetId}&size=A4&portrait=true&fitw=true&gridlines=false`;const token =ScriptApp.getOAuthToken();const pdfBlob =UrlFetchApp.fetch(url,{headers:{Authorization:'Bearer '+ token },}).getBlob().setName('Monthly Report.pdf');GmailApp.sendEmail('[email protected]','Monthly Report','Please find this month\'s report attached.',{attachments:[pdfBlob]});Logger.log('Report emailed as PDF.');}