node.js AWS Lambda 无法返回 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45348580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS Lambda fails to return PDF file
提问by sami_analyst
I have created a lambda function using serverless. This function is fired via API Gateway on a GET request and should return a pdf file from a buffer. I'm using html-pdfto create the buffer and trying to return the pdf file with the following command
我使用serverless创建了一个 lambda 函数。这个函数是通过 API 网关在 GET 请求上触发的,应该从缓冲区返回一个 pdf 文件。我正在使用html-pdf创建缓冲区并尝试使用以下命令返回 pdf 文件
let response = {
statusCode: 200,
headers: {'Content-type' : 'application/pdf'},
body: buffer.toString('base64'),
isBase64Encoded : true,
};
return callback(null, response);
but the browser is just failing to load the pdf, so I don't know exactly how to return the pdf file directly to the browser. Could'nt find a solution for that.
但浏览器只是无法加载 pdf,所以我不知道如何将 pdf 文件直接返回到浏览器。找不到解决方案。
回答by sami_analyst
well, I found the answer. The settings in my response object are fine, I just had to manually change the settings in API Gateway for this to work in the browser. I have added "*/*" to binary media types under the binary settings in API Gateway console
好吧,我找到了答案。我的响应对象中的设置很好,我只需要手动更改 API Gateway 中的设置即可在浏览器中工作。我在 API Gateway 控制台的二进制设置下为二进制媒体类型添加了“*/*”
API GATEWAY
API网关
- just log into your console
- choose your api
- click on binary support in the dropdown
- edit binary media type and add "*/*"
- 只需登录您的控制台
- 选择你的 API
- 单击下拉列表中的二进制支持
- 编辑二进制媒体类型并添加“*/*”
FRONTEND
前端
opening the api url in new tab (target="_blank"). Probably the browser is handling the encoded base 64 response, In my case with chrome, the browser just opens the pdf in a new tab exactly like I want it to do.
在新选项卡中打开 api url (target="_blank")。浏览器可能正在处理编码的 base 64 响应,在我使用 chrome 的情况下,浏览器只是按照我想要的方式在新选项卡中打开 pdf。
回答by teocomi
After spending several hours on this I found out that if you set Content handlingto Convert to binary(CONVERT_TO_BINARY) the entire response has to be base64, I would otherwise get an error: Unable to base64 decode the body.
在花了几个小时之后,我发现如果你设置Content handling为Convert to binary(CONVERT_TO_BINARY) 整个响应必须是 base64,否则我会得到一个错误:Unable to base64 decode the body。
Therefore my response now looks like:
因此,我的回应现在看起来像:
callback(null, buffer.toString('base64'));
callback(null, buffer.toString('base64'));
The Integration response:
集成响应:
The Method response:
方法响应:
And Binary Media Types:
和二进制媒体类型:
回答by dashmug
If you have a gigantic PDF, then it will take a long time for Lambda to return it and in Lambda you are billed per 100ms.
如果您有一个巨大的 PDF,那么 Lambda 将需要很长时间才能返回它,并且在 Lambda 中,您每 100 毫秒收费一次。
I would save it to S3 first then let the Lambda return the S3 url to the client for downloading.
我会先将它保存到 S3,然后让 Lambda 将 S3 url 返回给客户端以供下载。
回答by Prashant Tapase
Above solution is only for particular content-type. You can't more content type. Follow only below two-step to resolve multiple content type issue.
以上解决方案仅适用于特定的内容类型。你不能更多的内容类型。仅按照以下两步来解决多内容类型问题。
- Click on the checkbox of Use Lambda Proxy integration
- 单击“使用 Lambda 代理集成”复选框
API gateway --> API --> method --> integration request
API 网关 --> API --> 方法 --> 集成请求
Create your response as
let response = { statusCode: 200, headers: { 'Content-type': 'application/pdf',//you can change any content type 'content-disposition': 'attachment; filename=test.pdf' // key of success }, body: buffer.toString('base64'), isBase64Encoded: true }; return response;
将您的回复创建为
let response = { statusCode: 200, headers: { 'Content-type': 'application/pdf',//you can change any content type 'content-disposition': 'attachment; filename=test.pdf' // key of success }, body: buffer.toString('base64'), isBase64Encoded: true }; return response;
Note* - It is not secure
注意* - 它不安全


