从远程url加载pdf以Mandrill附件的形式发送

Load pdf from remote url to send as Mandrill attachment

本文关键字:Mandrill url 加载 pdf      更新时间:2023-09-26

我正在尝试在Cloud Code中加载一个Pdf,并将其作为附件与mandrill一起发送。

由于mandrill需要一个base64编码的字符串作为附件,我使用Buffer来更改编码。

现在的结果是无法打开Pdf附件,我认为错误是由我的重新编码引起的。

我猜get请求返回的文本是utf8,但我不确定。

有什么建议吗?

var Buffer=require('Buffer').Buffer;

function loadPdf(pdfUrl) {
  var promise = new Parse.Promise();
  Parse.Cloud.httpRequest({
    method: 'GET',
    url: pdfUrl,
    success: function (httpResponse) {
      // Put text into buffer
      var buf = new Buffer('httpResponse.text', 'utf8');
      // encode text with base64
      promise.resolve(buf.toString('base64'));
    },
    error: function (httpResponse) {
      console.error(httpResponse);
      console.error("Token Request failed with response code " + httpResponse.status);
    }
  });
  return promise;
}

该函数的结果被放入

"attachments": [
    {
        "type": "application/pdf",
        "name": "test.pdf",
        "content": encodePdfText
    }

我最近遇到了同样的问题,请尝试以下代码,确保httpResponse.text在utf8…中

"content": new Buffer(httpResponse.text.toString('base64')).toString()