问题描述
mongodb存取的word文件,需要在浏览器中显示,但浏览器不好显示office文件,所以目前的解决的方案是,在后台通过gridfs-stream读取word,然后转换为pdf
问题出现的环境背景及自己尝试过哪些方法
node.js
相关代码
const downloadFile = function (isPicture, req, res, next) {
req.checkParams('id', "路径参数id格式有误").isMongoId();
let id = req.params.id;
let errors = req.validationErrors();
if (errors) {
return res.error(errors[0].msg);
}
Services.Gridfs.getOne(id).then(function (file) {
if (file) {
res.header("Content-Type", file.contentType);
if (!isPicture) {
setContentDispositionHeader(req, res, file.filename);
}
let gfs = utils.Gridfs.getGridfs();
var readstream = gfs.createReadStream({
_id: id
});
readstream.on('error', function (error) {
next(error);
});
readstream.pipe(res);
} else {
return res.error("无此文件", 404);
}
}).catch(err=>next(err));
};
没有哪个数据库会帮你把word转换成PDF。读出来自己转,第三方工具很多,比如:Office to PDF
是不是适合你使用请自己验证。