GmailMessage to PDF

Google Apps Script - Gmail

Will the GmailMessage ( GmailThread ) .getAsPdf() method be implemented? The expected result will be the same as printing to PDF, available in Gmail. This feature is available on the website, so why not in Script?

This is necessary to quickly distribute selected Gmail conversations to others / external in PDF format.

In addition, GmailMessage.getAttachments() , although in online documents does not exist in reality. Will it be implemented?

thanks

+4
source share
1 answer

I tried this and worked well (not sure if this is the only approach):

 function getattach(){ var firstThread = GmailApp.getInboxThreads(0,1)[0]; var message = firstThread.getMessages()[0]; var attach = message.getAttachments(); Logger.log(attach[0].getDataAsString() ) if(attach.length>0){ var file=DocsList.createFile(attach[0]) var pdf=file.getAs('application/pdf').getBytes(); // for test purpose I send the pdf as attachment var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'}; MailApp.sendEmail(' emailadress@gmail.com ', 'Your test as PDF ', 'see attachment', {attachments:[attach_to_send]}); file.setTrashed(true);// delete after use ;-) } } 

EDIT 1: deleted

EDIT 2: there is a new version with the body in the pdf application, html is also supported (using the DocsList services), temporary documents are deleted. In a word: quite satisfying; -)

 function getAttachAndBody(){ var firstThread = GmailApp.getInboxThreads(0,1)[0]; var message = firstThread.getMessages()[0]; var attach = message.getAttachments(); var body = message.getBody();//is a string var bodydochtml = DocsList.createFile('body.html', body, "text/html") var bodyId=bodydochtml.getId() var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes(); if(attach.length>0){ var file=DocsList.createFile(attach[0]) var pdf=file.getAs('application/pdf').getBytes(); var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'}; var body_to_send = {fileName: 'body.pdf',content:bodydocpdf, mimeType:'application/pdf'}; MailApp.sendEmail(' emailadress@gmail.com ', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]}); file.setTrashed(true); DocsList.getFileById(bodyId).setTrashed(true) } } 
+2
source

All Articles