将通过API生成的PDF发送到外部服务器? [英] Send a PDF generated from an API to an external server?

查看:53
本文介绍了将通过API生成的PDF发送到外部服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NodeJS-ExpressJS服务器(用于托管网页)和Django API服务器.当从ExpressJS服务器调用Django服务器中的特定端点时(当用户单击网页上的按钮时),将生成PDF.我想将此PDF从Django服务器发送到ExpressJS服务器,然后将其下载到用户的浏览器(用户仅与ExpressJS服务器交互).为此,我在Django服务器中将PDF编码为base64字符串,并在HTTP响应中返回该字符串.在ExpressJS服务器中,我解码base64字符串,然后使用ExpressJS框架提供的 res.download()方法将PDF下载到用户的浏览器.此方法似乎可以工作,但是会损坏PDF文件吗?有没有一种方法可以简单地将PDF文件作为二进制文件发送并以这种方式下载到浏览器(如果可能,请提供示例)?任何答案,示例和建议都将不胜感激.

I have a NodeJS-ExpressJS server (to host a web page) and Django API server. When a specific endpoint in the Django server is called from the ExpressJS server (when a user clicks a button on the web page), a PDF is generated. I want to send this PDF from the Django server to the ExpressJS server and then download it to the user's browser (The user only interfaces with ExpressJS server). In order to do this I encode the PDF as a base64 string in the Django server and return the string in an HTTP response. In the ExpressJS server, I decode the base64 string and then use the res.download() method provided by the ExpressJS framework to download the PDF to the user's browser. This method appears to work, but can it corrupt the PDF file? Is there a way to simply send the PDF file as a binary file and download it to the browser that way (If possible, please provide an example)? Any answers, examples, and suggestions are greatly appreciated.

Django API server
def process_upload(request):
   '''
   Process request and generate PDF
   ....
   '''
   with open(pdf, "rb") as pdf_file:
     encoded_string = base64.b64encode(pdf_file.read())
     return HttpResponse(encoded_string)

ExpressJS server
server.get('/api', function(req, res, next) {
  request.post({
     url: 'http://<api_url>',
     oauth: oauth,
     preambleCRLF: true,
     postambleCRLF: true
   }, function(error, response, body){
      res.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Content-Disposition': 'attachment; filename="file.pdf"' 
      });
   });
   const download = Buffer.from(body.toString('utf-8'), 'base64');
   res.end(download);
 });  

推荐答案

我认为您的代码很庞大,并且会将整个 file.pdf 缓冲到内存( body )中对于将结果写回到客户端之前的每个请求,如果同时有多个请求,可能会开始消耗大量内存,这就是为什么应该使用流的原因:

I think your code is bulky and buffers up the entire file.pdf into memory (body) for every request before writing the result back to clients, it could start eating a lot of memory if there were many requests at the same time, that's why you should use streams:

server.get('/apisd', function(req, res, next) {

    // Force browser to download file
    res.set('Content-Type', 'application/pdf');
    res.set('Content-Disposition', 'attachment; filename=file.pdf');

    // send file
    request.post({
        url: 'http://<api_url>',
        oauth: oauth,
        preambleCRLF: true,
        postambleCRLF: true
    }).pipe(res);

});

通过使用流,该程序将一次读取 file.pdf 一块,并将其存储到内存中,然后将其发送回客户端.

By using streams the program will read file.pdf one chunk at a time, store it into memory and send it back to the client.

这篇关于将通过API生成的PDF发送到外部服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆