Add encryption support and access privileges #2696
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, @diegomura! Love your library - I've been using it extensively at https://github.com/klimeryk/recalendar.js/ (https://recalendar.me/). I stumbled upon #672 and figured it was a great chance to contribute back :)
I've tried my best to bring over the changes from the upstream pdfkit version. Encryption was originally added in foliojs/pdfkit#820 there. It was mostly a straightforward update... except changes in
reference.js
. Looks like that class has been fundamentally changed - upstream it uses internallyBuffer
, while in react-pdf, the class extendsWriteable
instead.I've tried first keeping the changes to
reference.js
minimal (see 4376bcb). Most of the changes were easy to port - basically copy and paste. The only part I did not know was the changes infinalize
inreference.js
here. As a result, this was a partial success - non-encrypted PDFs look as before (good), you can generate a PDF that requires a password, the password is properly validated... but then the opened PDF is missing content. See this file: recalendar-password.pdf (password:secret
)I've then tried just switching to upstream's
reference.js
, especially seeing your intention of doing that from #2613. See 5d9a8e9. That looks more promising - the decrypted PDF has the text and almost looks perfect... except, for some reason, the font seems to be missing, so it looks off. See this file: recalendar-password2.pdf (password:secret
)And this applies to both encrypted and non-encrypted generated PDFs. So I'm guessing there's some modification/customization, maybe in some other file that I missed that was done in this version of pdfkit that is not compatible with the upstream version of
reference.js
? Unfortunately, I could not find it. I've tried also going back in history ofreference.js
, but unfortunately these changes seems to be from before it was moved to monorepo and I cannot find the previous repository 馃槥I figured I'd create this PR for you to have a look, maybe something will spark a quick solution or hint from you. If not, I can continue digging 馃檱
Testing
I'm guessing you have your preferred ways of testing different use cases :) Enabling password protection simply boils down to passing
userPassword
when creating a new instance ofPDFDocument
like so:To be on the safe side, I've also tried enabling all optional permissions, but that did not make a difference: