【问题标题】:Load pdf from remote url to send as Mandrill attachment从远程 url 加载 pdf 以作为 Mandrill 附件发送
【发布时间】:2014-11-16 09:54:25
【问题描述】:

我正在尝试在 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
    }

【问题讨论】:

    标签: javascript pdf parse-platform mandrill


    【解决方案1】:

    我最近也遇到了同样的问题,试试下面的代码,确保httpResponse.text是utf8...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      相关资源
      最近更新 更多