Add Password to pdf after using unoconv



  • I manage to add password to a converted pdf (unoconv) using a script and static-pdf ,
    Now trying to move the script to be global and getting error - for the same script,
    Can you assist ? @bjrmatos @jan_blaha

    https://playground.jsreport.net/w/benben-user/KNTuB2lm

    const jsreport = require('jsreport-proxy')
    async function afterRender(req, res) {
        if (req.data?.password) {
            myjson = {
                "template": {
                    "recipe": "static-pdf",
                    "content": "",
                    "engine": "handlebars",
                    "staticPdf": {
                        "pdfAsset": {
                            "content": res.content.toString('base64'),
                            "encoding": "base64"
                        }
                    },
                    "pdfPassword": {
                        "password": req.data?.password,
                        "ownerPassword": req.data?.password
    
                    }
                }
            }
            const result = await jsreport.render(myjson)
            res.content = result.content;
        }
    }
    

    error
    0_1676445752745_upload-ba941fb9-22bf-43f0-8dc5-4553ca92e0ba



  • That is because the global script is in this case invoked twice.
    Once for the original template and once for the static-pdf template invoked in the script. If it wouldn't fail, it would end in an endless loop.

    What you need to do is skip the script for the template invoked from the script.
    For example like this

    const jsreport = require('jsreport-proxy')
    
    
    async function afterRender(req, res) {
        if (req.data?.password && !req.context.isChildRequest) {
            myjson = {
                "template": {
                    "recipe": "static-pdf",
                    "content": "",
                    "engine": "handlebars",
                    "staticPdf": {
                        "pdfAsset": {
                            "content": res.content.toString('base64'),
                            "encoding": "base64"
                        }
                    },
                    "pdfPassword": {
                        "password": req.data?.password,
                        "ownerPassword": req.data?.password
    
                    }
                }
            }
            const result = await jsreport.render(myjson)
            res.content = result.content;
        }
    }
    


  • @jan_blaha TY for the quick response 🙏 Working now
    A better option is to implement this in the unoconv extension as additional input and use unoconv password option (although it is deprecated )



  • @jan_blaha still having issue.. not working using API

    Cannot set property offset of [object Object] which has only a getter

    0_1676457889430_upload-a6a7c31a-d109-4fbf-9164-bdd4ba07572a



  • Exactly the same as in the playground?
    What does your request look like?
    You probably set something wrong in the script to the req/res.



  • @jan_blaha same code ,then i just add this line :

    async function afterRender(req, res) {}
    

    and got the error ..



  • This post is deleted!


  • I don't know how to replicate that, please elaborate more.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.