问题
I am using a library called Node Jasper . Pdf is also getting generated via dynamically passed parameters. I want to save that file in project folder. Can some one help me how to achieve that ? Below is the snipet
exports.jasperdemo = (req, res) => {
console.log(req.body)
var report = {
report: 'hw',
data: {
parameter1: req.body.parameter1,
parameter2: req.body.parameter2
},
}
var pdf = jasper.pdf(report);
res.set({
'Content-type': 'application/pdf',
'Content-Length': pdf.length
});
fs.writeFile("Test.pdf",res.body);
res.send(pdf)
}
回答1:
create a buffer of binary data using new Buffer(pdf,"binary")
and pass it instead of res.body find the attached code
exports.jasperdemo = (req, res, next) => {
var report = {
report: 'hw',
data: {
parameter1: req.body.parameter1,
parameter2: req.body.parameter2
}
}
var pdf = jasper.pdf(report);
res.set({
'Content-type': 'application/pdf',
'Content-Length': pdf.length
});
fs.writeFile("./reports/Test.pdf",new Buffer(pdf, 'binary'), ()=>{
console.log('successful');
});
res.send(pdf)
}
回答2:
You can just add the directory before the file name in your fs.writeFile()
function:
exports.jasperdemo = (req, res) => {
console.log(req.body)
var report = {
report: 'hw',
data: {
parameter1: req.body.parameter1,
parameter2: req.body.parameter2
},
}
var pdf = jasper.pdf(report);
res.set({
'Content-type': 'application/pdf',
'Content-Length': pdf.length
});
// Added your change here
fs.writeFile("./your/directory/Test.pdf",res.body);
res.send(pdf)
}
来源:https://stackoverflow.com/questions/54433215/how-to-save-generated-pdf-in-project-folder-node-jasper-reports