How to save generated pdf in project folder Node Jasper Reports?

与世无争的帅哥 提交于 2021-02-09 11:14:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!