pdfly icon indicating copy to clipboard operation
pdfly copied to clipboard

ENH: meta command - Add Permissions + fix version read on encrypted PDFs

Open iamrishu11 opened this issue 1 year ago • 8 comments

Fixes #19

Description

What’s new

  • pdfly meta now shows a compact, single-line Permissions field in the PDF Data table.
  • Unencrypted PDFs show n/a (unencrypted).
  • Encrypted PDFs show a comma-separated list of the allowed permissions (e.g., print, modify, annotate, fill-forms). If none are allowed, it shows none (all denied).

Why

  • Addresses the feature request to display document permissions and follows the maintainer’s suggestion to keep it on a single line. Fixes #19.

How

  • Uses PdfReader.user_access_permissions from pypdf.
  • New helper _format_permissions(...)
  • Also fixes header reading for encrypted PDFs by seeking to position 0 before reading the %PDF-… version.

Tests

  • Manual verification included (screenshots in this PR)

For an encrypted pdf

image

For a normal file

image

Notes

  • Output order of permissions is alphabetical for stability.

Checklist:

  • [x] A unit test is covering the code added / modified by this PR

  • [ ] In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder

  • [x] A mention of the change is present in CHANGELOG.md

  • [x] This PR is ready to be merged

By submitting this pull request, I confirm that my contribution is made under the terms of the BSD 3-Clause license.

iamrishu11 avatar Oct 15 '25 12:10 iamrishu11

I'm not sure if this 2nd table visible in your screenshot is needed, given that the same information is visible in the 1st table: img

Lucas-C avatar Oct 15 '25 13:10 Lucas-C

alr, removing the diff table and adding unittest and changelogs

iamrishu11 avatar Oct 15 '25 13:10 iamrishu11

Quick sanity check on the test fixture paths

I’m planning to use the following in the tests:

from pathlib import Path

rel = Path("sample-files/002-trivial-libre-office-writer/002-trivial-libre-office-writer.pdf") input_pdf = RESOURCES_ROOT / rel

Assuming RESOURCES_ROOT points to the repo root (as in tests/conftest.py), this resolves to:

/sample-files/002-trivial-libre-office-writer/002-trivial-libre-office-writer.pdf

…which is the expected location in the sample-files submodule. If RESOURCES_ROOT already points inside sample-files/, I’ll drop the leading sample-files/ from rel.

The test also pytest.skip(...)s if the file isn’t present, and the submodule can be initialized with:

git submodule update --init --recursive

Shout if you prefer me to reference it as:

rel = Path("002-trivial-libre-office-writer/002-trivial-libre-office-writer.pdf") input_pdf = RESOURCES_ROOT / "sample-files" / rel

Either style works; just want to match the project’s convention.

iamrishu11 avatar Oct 15 '25 15:10 iamrishu11

Quick sanity check on the test fixture paths [...] Either style works; just want to match the project’s convention.

Let's got for this:

SAMPLE_FILES = RESOURCES_ROOT / "sample-files"
input_pdf = SAMPLE_FILES / rel

Lucas-C avatar Oct 15 '25 15:10 Lucas-C

I made a final feedback comment, but appart from that everything looks really good! 👍

I'll merge your PR once you have addressed that last point and the CI pipeline is passing.

Lucas-C avatar Oct 15 '25 15:10 Lucas-C

Hey lucas, can u check now?

iamrishu11 avatar Oct 16 '25 08:10 iamrishu11

The unit tests are failing:

ERROR tests/test_metadata.py - ValueError: tests/test_metadata.py::test_meta_command_on_all_sample_pdfs: error raised while trying to determine id of parameter 'input_pdf' at position 0

Lucas-C avatar Oct 16 '25 11:10 Lucas-C

Hi @iamrishu11 🙂 👋 We are close to the end of the month, I was just curious to know if you were willing to finish this PR as part of Hacktoberfest?

Lucas-C avatar Oct 27 '25 07:10 Lucas-C