galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Creating encrypted pdf files #353

Closed michaelktree closed 5 years ago

michaelktree commented 5 years ago

Hi, My scenario is, I have pdf templates that I modify according to user inputs. The template pdf files are not encrypted, but I want to return a locked file that cannot be updated after inserting user data (but I don't want to lock the file with a password). I am using this command to create a new file, and add to it some fields:

var pdfWriter = hummus.createWriterToModify(
    new hummus.PDFRStreamForBuffer(pdf_template),
    new hummus.PDFWStreamForFile(tempDirPath + '/dev5.pdf'),            
    {
        userPassword: '',
        ownerPassword: '111',
        userProtectionFlag: 4                   
    }            
);

: : : await pdfWriter.writePage(page); await pdfWriter.end();

Everything seems to work fine, however the output pdf is not encrypted...

I tried another route to achieve this using "recrypt" function, but that has its own issue: When using it on an 'existing' file it works fine. but when using it in the same process, on the output of the createWriterToModify above, I get an exception: "Unable to recrypt files, check that input and output files are clear and arguments are coool"

Any help would be appreciated. Thanks, Michael

michaelktree commented 5 years ago

So I read this: https://github.com/galkahana/HummusJS/issues/216 According to that my only option is to make two passes:

  1. modify the document and then
  2. recrypt it This doesn't work for some reason. I get the following exception when trying to recrypt a file modified by createWriterToModify: "Unable to recrypt files, check that input and output files are clear and arguments are coool" When trying to recrypt an existing file from a previous session it works great. It does not work only when the createWriterToModify and the recrypt are run one after the other... Any ideas? Thanks
michaelktree commented 5 years ago

Hi @galkahana, Any solution or insight for this would be great. Thanks!

michaelktree commented 5 years ago

Well, I'm still not sure why I could not achieve this via Hummus, However I found out that this works when trying it with HummusRecipe (which relies on Hummus)