2

I'm using the Gmail API to get an attachment that is a pdf file.

The Gmail API returns the attachment as a base64-encoded string.

Is there a way to convert the base64-encoded string (the pdf document) directly to a string?

    function getAttachment(auth, attachmentId, messageId) {
      const gmail = google.gmail({version: 'v1', auth});
      gmail.users.messages.attachments.get({
        id: attachmentId,
        messageId: messageId,
        userId: 'me',
      }, async (err, res) => {
      if (err) {
        console.log(err);
      }
      console.log(res.data);
      const pdfAsString = Buffer.from(res.data.data, 'base64').toString('ascii')
      console.log(pdfAsString); 
     });
   }

The Gmail API returns the following data
console.log(res.data).
Note that I'm not pasting the whole string here

{
size: 13409,
data: 'JVBERi0xLjukJb_...lJUVPRgo='
}

Is this possible? If not what are my other options?

jps
  • 20,041
  • 15
  • 75
  • 79
danielfrs87
  • 117
  • 2
  • 6

0 Answers0