Guest User

Save As PDF

a guest
Nov 20th, 2013
1,765
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function save_Gmail_as_PDF(){
  2. var label = GmailApp.getUserLabelByName("Save As PDF");
  3. if(label == null){
  4. GmailApp.createLabel('Save As PDF');
  5. }
  6. else{
  7. var threads = label.getThreads();
  8. for (var i = 0; i < threads.length; i++) {
  9. var messages = threads[i].getMessages();
  10. var message = messages[0];
  11. var body = message.getBody();
  12. var subject = message.getSubject();
  13. var attachments = message.getAttachments();
  14. for(var j = 1;j<messages.length;j++){
  15. body += messages[j].getBody();
  16. var temp_attach = messages[j].getAttachments();
  17. if(temp_attach.length>0){
  18. for(var k =0;k<temp_attach.length;k++){
  19. attachments.push(temp_attach[k]);
  20. }
  21. }
  22. }
  23. // Create an HTML File from the Message Body
  24. var bodydochtml = DocsList.createFile(subject+'.html', body, "text/html")
  25. var bodyId=bodydochtml.getId();
  26.  
  27. // Convert the HTML to PDF
  28. var bodydocpdf = bodydochtml.getAs('application/pdf');
  29. if(attachments.length > 0){
  30. DocsList.createFolder(subject);
  31. var folder = DocsList.getFolder(subject);
  32. for (var j = 0; j < attachments.length; j++) {
  33. folder.createFile(attachments[j].getName(),attachments[j].getBytes());
  34. }
  35. folder.createFile(bodydocpdf);
  36. }
  37. else{
  38. DocsList.createFile(bodydocpdf);
  39. }
  40. DocsList.getFileById(bodyId).setTrashed(true);
  41. label.removeFromThread(threads[i]);
  42. }
  43. }
  44. }
RAW Paste Data