javascript - 从 httpResponse 获取包含 pdf 的缓冲区并将其用作附件

标签 javascript node.js pdf parse-platform sendgrid

我正在尝试从包含 pdf 的远程 URL 发出 Get 请求,并从其响应中获取包含原始数据的缓冲区,并将其用作 sendGrid 的附件,但这似乎不起作用,因为发送的 pdf 是空的。

下面找到我使用的代码片段:

Parse.Cloud.httpRequest({ url:"http://<MY_URL>/x.pdf",
    headers:{
        "X-Parse-Application-Id":"<MY_ID>",
        "X-Parse-REST-API-Key": "<MY_KEY>",
    } 
}).then(function(httpResponse) {
    //email.addFileFromBuffer('x.pdf', httpResponse.buffer); 
    //-- Doesn't work raise and exception
    email.addFileFromBuffer(request.params.toName+'_report.pdf', new Buffer(httpResponse.text));
    sendgrid.send(email).then(function() {
        response.success("Email sent!");
    }, function(e) {
        console.log(e);
        response.error("Uh oh, something went wrong with sending email");
    });
}, function(e) {
    console.log(e);
    response.error("Uh oh, something went wrong with pdf");
});

如果有人对此有不同的想法。

非常感谢!

编辑1:

使用时:

email.addFileFromBuffer(request.params.toName+'_report.pdf', httpResponse.buffer);

结果:

Ran cloud function sendEmail with:
Input: {"fileName":"x.pdf","toEmail":"xxx@gmail.com","toName":"XXX"}
Result: URIError: URI malformed
at encodeURIComponent (native)
at <anonymous>:325:38
at Parse.js:1:21050
at Array.forEach (native)
at Function.x.each.x.forEach (Parse.js:1:661)
at b._objectEach.b._each (Parse.js:1:21023)
at formEncode (<anonymous>:321:5)
at Object.Parse.Cloud.httpRequest (<anonymous>:532:24)
at SendGrid.send (sendgrid.js:292:19)
at main.js:29:12

最佳答案

Parse.Cloud.httpRequest 返回一个具有原始字节和字符串表示形式的 response 对象。您不需要创建新的缓冲区,只需使用 response.buffer

挖掘它:

email.addFileFromBuffer(request.params.toName+'_report.pdf', httpResponse.buffer);

关于javascript - 从 httpResponse 获取包含 pdf 的缓冲区并将其用作附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041175/

相关文章:

javascript - socketio 客户端静静地失去连接并创建新的套接字(传输关闭)

node.js - 如果nodejs中不需要执行任何操作,跳过调用回调是否安全

pdf - 大型pdf文件的高效缩略图生成?

javascript - 在 javascript 中通过 D-click 将参数传递给应用程序

javascript - 获取指令函数属性的参数并在另一个 Controller AngularJS中更改它

javascript - Access-Control-Allow-Origin 不允许来源 http ://XXXXX. com

javascript - 具有多个操作的三元运算符

php - 在dompdf中创建pdf后如何打开打印窗口?

objective-c - 从 Core Data 到 PNG 的图像资源路径

javascript - <富 :calendar> client side disable weekends