无法加载pdf文档,NodeJs [英] Failed to load pdf document, NodeJs

查看:163
本文介绍了无法加载pdf文档,NodeJs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模拟的测试数据下已经存在的pdf在没有提示的情况下下载pdf.我可以使用以下代码完成上述任务.但是,当我尝试打开下载的pdf文件时,我得到的是无法加载pdf文档",在这里我会丢失.

I am trying to download a pdf without prompt using the pdf which is already present under mocked test data. And I am able to do the above task using the below code.But when I try to open the downloaded pdf I am getting 'Failed to load pdf document' what am I missing here.Kindly help.

fs.readFile('./src/test-data/service/print/notes.pdf', function(error, content) {
    if (error) {
        res.writeHead(500);
        res.end();
    }
    else {
        res.writeHead(200, {
            'Content-Type': 'application/pdf',
            'Content-Disposition': 'attachment; filename=notes.pdf'
        });
        res.end(content, 'utf-8');
    }
});

推荐答案

这很正常.

var file = fs.createReadStream('./src/test-data/service/print/notes.pdf');
var stat = fs.statSync('./src/test-data/service/print/notes.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=agreement.pdf');
file.pipe(res);

这篇关于无法加载pdf文档,NodeJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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