nrhirani / node-qpdf

A Content Preserving transformations on PDFs wrapped around QPDF
MIT License
51 stars 54 forks source link

No such file or directory - file name contains a space #16

Open evolutionxbox opened 3 years ago

evolutionxbox commented 3 years ago

Please note that I am using node-qpdf2, but that's just a wrapper around this module.

Trying to encrypt a file that has one or more spaces in the name

qpdf
    .encrypt(
      '/Users/my/absolute/path/to/A Sample PDF.pdf',
      {
        keyLength: 40,
        password: {
          user: '',
          owner: 'YOUR_PASSWORD_TO_ENCRYPT_FOR_OWNER',
        },
        restrictions: {
          print: 'y',
        },
      },
      '/Users/my/absolute/path/to/protected.pdf'
    )
    .then((x) => console.log('Done: ', x))
    .catch((e) => console.error('Error: ', e));

Result:

Error:  open /Users/my/absolute/path/to/A\ Sample\ PDF.pdf: No such file or directory

I thought that maybe adding double quotes around the path would help, as that is what you do in the shell to allow paths with spaces:

qpdf
    .encrypt(
      '"/Users/my/absolute/path/to/A Sample PDF.pdf"',
      {
        keyLength: 40,
        password: {
          user: '',
          owner: 'YOUR_PASSWORD_TO_ENCRYPT_FOR_OWNER',
        },
        restrictions: {
          print: 'y',
        },
      },
      '/Users/my/absolute/path/to/protected.pdf'
    )
    .then((x) => console.log('Done: ', x))
    .catch((e) => console.error('Error: ', e));

Result:

Done:  Input file doesn't exist

Using path.resolve also results in the "Input file doesn't exist" error.