AWS Lambda无法返回PDF文件 [英] AWS Lambda fails to return PDF file
问题描述
我已经使用 serverless 创建了一个lambda函数.该函数通过API网关在GET请求上触发,并应从缓冲区返回pdf文件.我正在使用 html-pdf 创建缓冲区,并尝试使用以下命令
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-pdf to create the buffer and trying to return the pdf file with the following command
let response = {
statusCode: 200,
headers: {'Content-type' : 'application/pdf'},
body: buffer.toString('base64'),
isBase64Encoded : true,
};
return callback(null, response);
但是浏览器只是无法加载pdf,所以我不知道确切如何将pdf文件直接返回到浏览器.无法找到解决方案.
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.
推荐答案
好,我找到了答案. 我的响应对象中的设置很好,我只需要手动更改
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网关
- 只需登录您的控制台
- 选择您的api
- 单击下拉列表中的二进制支持
- 编辑二进制媒体类型并添加"*/*"
前锋
在新标签页中打开api网址(target ="_ blank").可能是浏览器正在处理已编码的base 64响应,就我而言,对于chrome,浏览器只是在新标签页中打开pdf文件,就像我想要的那样.
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.
这篇关于AWS Lambda无法返回PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!