node.js 如何将 pdf 文件从 Node/Express 应用程序发送到浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31105846/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:55:44  来源:igfitidea点击:

How to send a pdf file from Node/Express app to the browser

node.jsexpress

提问by Eugene Goldberg

In my Node/Express app I have the following code, which suppose to read a PDF document from a file, and send it to the browser:

在我的 Node/Express 应用程序中,我有以下代码,假设从文件中读取 PDF 文档,并将其发送到浏览器:

var file = fs.createReadStream('./public/modules/datacollectors/output.pdf', 'binary');
var stat = fs.statSync('./public/modules/datacollectors/output.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=quote.pdf');
res.pipe(file, 'binary');
res.end(); 

I do not get any errors, but I do not get the file in my browser either. What am I doing wrong?

我没有收到任何错误,但我的浏览器中也没有收到该文件。我究竟做错了什么?

回答by hassansin

You have to pipe from Readable Stream to Writable stream not the other way around:

您必须通过管道从 Readable Stream 到 Writable 流,而不是相反:

var file = fs.createReadStream('./public/modules/datacollectors/output.pdf');
var stat = fs.statSync('./public/modules/datacollectors/output.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=quote.pdf');
file.pipe(res);

Also you are setting encoding in wrong way, pass an object with encoding if needed.

此外,您以错误的方式设置编码,如果需要,请传递带有编码的对象。

回答by Wenlong Jiang

I think I found your answer in another post Display PDF file in a browser using node js.

我想我在使用 node js 在浏览器中显示 PDF 文件的另一篇文章中找到了您的答案。

After testing your code in Chrome, it immediately starts the download of the PDF file. But if you want to display the content of the PDF file you could try below:

在 Chrome 中测试您的代码后,它会立即开始下载 PDF 文件。但是,如果您想显示 PDF 文件的内容,您可以尝试以下操作:

var data =fs.readFileSync('./public/modules/datacollectors/output.pdf');
res.contentType("application/pdf");
res.send(data);

This should directly send PDF content into the browser.

这应该直接将 PDF 内容发送到浏览器中。

Hope this answers your question.

希望这能回答你的问题。

回答by Renuka Misal

Best way to download a PDF on REST API call.

在 REST API 调用上下载 PDF 的最佳方式。

var path = require('path');     
var file = path.join(__dirname, 'file.pdf');    
res.download(file, function (err) {
       if (err) {
           console.log("Error");
           console.log(err);
       } else {
           console.log("Success");
       }    
});

回答by Christopher Chalfant

Here's the easiest way:

这是最简单的方法:

app.get('/', (req, res) => res.download('./file.pdf'))

If this gives you trouble. Check the Express.js version or any middlewares that might be necessary.

如果这给你带来麻烦。检查 Express.js 版本或任何可能需要的中间件。

Cheers

干杯

回答by kdhayal

If you are using the Swagger/OpenAPI then you can look in responses section of below code. /api/pdf_from_html: post: tags: - PDF description: Create PDF from html produces: - application/pdf consumes: - application/json parameters: - name: contents description: HTML content to convert to pdf in: body required: true schema: $ref: "#/definitions/pdf" responses: 200: description: Returns a PDF encoded in base64 content: application/pdf: schema: type: string format: base64

如果您使用的是 Swagger/OpenAPI,那么您可以查看以下代码的响应部分。 /api/pdf_from_html: post: tags: - PDF description: Create PDF from html produces: - application/pdf consumes: - application/json parameters: - name: contents description: HTML content to convert to pdf in: body required: true schema: $ref: "#/definitions/pdf" responses: 200: description: Returns a PDF encoded in base64 content: application/pdf: schema: type: string format: base64