2

我正在使用 Gmail API 来获取一个 pdf 文件的附件。

Gmail API 将附件作为 base64 编码的字符串返回。

有没有办法将base64编码的字符串(pdf文档)直接转换为字符串?

    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); 
     });
   }

Gmail API 返回以下数据
console.log(res.data)。
请注意,我没有在此处粘贴整个字符串

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

这可能吗?如果不是,我的其他选择是什么?

4

0 回答 0