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_blahahttps://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
-
That is because the global script is in this case invoked twice.
Once for the original template and once for thestatic-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 thisconst 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
-
Exactly the same as in the playground?
What does your request look like?
You probably set something wrong in the script to thereq/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.