hummusRecipe icon indicating copy to clipboard operation
hummusRecipe copied to clipboard

Modifying a password-protected PDF with text changes (StartCopyingContext error)

Open thmclellan opened this issue 4 years ago • 0 comments

First off - thank you John for this amazing library! It's made Hummus much easier.

I'm trying to add text to an existing PDF that is protected with a user password and leave the password protection in place while making changes.

Everything goes well until I call endPDF(), which triggers the following error:

TypeError: unable to create copying context. verify that the target is an existing PDF file

The hummus-error.log says this:

PDFDocumentHandler::StartCopyingContext, Cant decrypt document. make sure to provide appropriate password for this document in order to copy from it

It seems the error happens when endPDF() calls this._writeInfo(), which calls hummus's writer.createPDFCopyingContext(), an older Hummus function that doesn't seem to accept a password.

https://github.com/chunyenHuang/hummusRecipe/blob/198f9b88044120c5bf16f039734e30f6ba539318/lib/Recipe.js#L206

https://github.com/chunyenHuang/hummusRecipe/blob/198f9b88044120c5bf16f039734e30f6ba539318/lib/info.js#L33

For my own purposes, when I comment out the writeInfo call, everything seems to work okay, but I'm guessing writeInfo may be important for other use cases.

In a future update, would it make sense to make the writeInfo step optional somehow? (e.g. as an option when calling endPDF or when instantiating HummusRecipe). Or maybe there's an easier way that I'm not seeing. In any case, I wanted to capture the issue and the potential workaround.

Here is a sample PDF file with user password '123123' for testing: https://www.dropbox.com/s/vcodzkyv5s67mp3/pdf-with-password-123123.pdf?dl=1

thmclellan avatar Jun 05 '20 18:06 thmclellan