AWS Lambda 无法返回 PDF 文件 [英] AWS Lambda fails to return PDF file
问题描述
我使用 serverless 创建了一个 lambda 函数.这个函数是通过 API 网关在 GET 请求上触发的,应该从缓冲区返回一个 pdf 文件.我正在使用 html-pdf 创建缓冲区并尝试返回 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.
推荐答案
好吧,我找到了答案.我的响应对象中的设置很好,我只需要手动更改 settings 在 API Gateway 中使其在浏览器中工作.我已经添加了*/*"API Gateway 控制台中二进制设置下的二进制媒体类型
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 url(目标 =_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屋!